python事件处理_在Python中集成两个事件处理程序

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:

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)

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)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值