Integrating two event handling programs in Python

£可爱£侵袭症+ 提交于 2019-12-13 02:52:46

问题


I have two programs in Python. Let's call them Prog1.py and Prog2.py . Prog1 is helping me get live data from an external device. As of now, as and when it gets the data it prints it on the screen. There is a while loop running where in each iteration it prints out the new set of data acquired. Now Prog2 is a Python program built using PyQtGraph which is intended to plot the data real time like that of a monitor. As of now what Prog2 does is that (that is, when it is run separately), it plots the already acquired data. This is a moving plot. So Prog2.py has an update function which is called repeatedly by a timer at specified intervals so as to update the graph with next data point (to make it move towards the right) . My objective is to link Prog1 and Prog2 - that is somehow I want to pipe the data acquired in real time by Prog1 to Prog2 so that Prog2 can plot the data in real time.

Since Prog1 and Prog2 are independent events having their own event-loop process I am not sure how to link them. One naive idea I thought was to run Prog1.py and Prog2.py in parallel and ask Prog1.py to save the data to a file (maybe pickle it? ) and ask Prog2.py to read from it and then plot the graph. But I am not convinced by this. This looks dirty. Somehow I want to run the entire Prog2.py code inside Prog1.py. Is there a clean way to do this?

EDIT- Code

Prog1.py

/*

Code to get data from USB

*/

def main():
    while 1:
        data = get_data()
        print data

main()

Prog2.py is the PyQtGraph code via to timer to update the graph and make it scrolling


回答1:


You a few options, probably any of which would work:

  1. Join the programs into one, with a worker thread that monitors for data and sends this back to the main thread for plotting. (see: https://groups.google.com/forum/#!msg/pyqtgraph/haiJsGhxTaQ/RkOov_UZEcYJ)
  2. Join the programs into a single-threaded program, with a timer that checks for new data, and replots whenever data has arrived:

    plt = pg.plot()
    def update():
        data = getNewData()
        if data is not None:
            plt.plot(data, clear=True)
    timer = QTimer()
    timer.timeout.connect(update)
    timer.start(10)
    
  3. Keep the programs separate and use some form of IPC--sockets, pipes, etc. to communicate between them. This would involve serializing the data (probably with pickle, as you suggest). pyqtgraph.multiprocess even lets you send Qt signals between processes. This is probably the most difficult option, but here's an easy example:

    import pyqtgraph as pg
    import pyqtgraph.multiprocess as mp
    proc = mp.QtProcess() # start second process for plotting
    rpg = proc._import('pyqtgraph') 
    plt = rpg.plot() # create a PlotWidget in remote process
    
    # Unlike the last example, we can use a wile-loop here because the Qt
    # event loop is running in another process.
    while True:
        data = getNewData()
        if data is not None:
            plt.plot(data, clear=True, _callSync='off')
    


来源:https://stackoverflow.com/questions/24332162/integrating-two-event-handling-programs-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!