在Python中集成两个事件处理程序 [英] Integrating two event handling programs in Python

查看:237
本文介绍了在Python中集成两个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有两个程序。我们称之为Prog1.py和Prog2.py。 Prog1正在帮助我从外部设备获取实时数据。到目前为止,当它获取数据时,它会在屏幕上打印。有一个循环运行,在每次迭代中,它打印出新的数据集。现在Prog2是一个使用PyQtGraph构建的Python程序,它旨在实时绘制一个像监视器那样的数据。到目前为止,Prog2做的是(即,当它单独运行)时,它绘制已经获取的数据。这是一个动人的情节。所以Prog2.py有一个更新功能,它被定时器以指定的间隔重复调用,以便用下一个数据点更新图表(使其向右移动)。我的目标是链接Prog1和Prog2 - 这是我想要通过Prog1实时获取的数据到Prog2,以便Prog2可以实时绘制数据。



由于Prog1和Prog2是具有自己的事件循环过程的独立事件,我不知道如何链接它们。一个天真的想法,我想要并行运行Prog1.py和Prog2.py,并要求Prog1.py将数据保存到一个文件(可能是腌制?),并询问Prog2.py从中读取,然后绘制图形。但我不相信这个。这看起来很脏不知怎的,我想在Prog1.py中运行整个Prog2.py代码。有没有干净的方法呢?



EDIT-
代码



Prog1.py

  / * 

从USB获取数据的代码

* /

def main():
while 1:
data = get_data()
打印数据

main()

Prog2.py是通过定时器更新图形并使其滚动的PyQtGraph代码

解决方案

你有几个选择,可能其中任何一个可以工作:


  1. 将程序加入到一个工作线程中,监视数据并将其发送回主线程进行绘图。 (请参阅: https://groups.google.com/forum/#!将程序加入一个单线程程序,一个定时器可以检查新的数据,并且只要数据已经有了,就重新编程。到达:

      plt = pg.plot()
    def update():
    data = getNewData )
    如果数据不是无:
    plt.plot(data,clear = True)
    timer = QTimer()
    timer.timeout.connect(update)
    timer.start(10)


  2. 将程序分开并使用某种形式的IPC-插座,管道等之间进行沟通。这将涉及序列化数据(可能与酸洗,按照您的建议)。 pyqtgraph.multiprocess 甚至可以在进程之间发送Qt信号。这可能是最困难的选择,但这是一个简单的例子:

      import pyqtgraph as pg 
    import pyqtgraph.multiprocess作为mp
    proc = mp.QtProcess()#开始第二个进程绘制
    rpg = proc._import('pyqtgraph')
    plt = rpg.plot()#在远程中创建一个PlotWidget进程

    #与最后一个例子不同,我们可以在这里使用一个wile-loop,因为Qt
    #事件循环正在另一个进程中运行。
    while True:
    data = getNewData()
    如果数据不是None:
    plt.plot(data,clear = True,_callSync ='off')



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

解决方案

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')
    

这篇关于在Python中集成两个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆