我现在正在为即将到来的项目设计程序结构,我被困在以下问题上:
归结到它最重要的元素,我的程序被分成两个文件logic.py和oscilloscope.py。
在logic.py中,我在/dev/input/..上初始化一个设备,并设置用于信号处理的滤波器等。在oscilloscope.py中,我有一个示波器实现,它使用Pygame来解释/显示来自注册输入设备的输入。
现在,我在logic.py中有了一个主循环,它不断地监听来自设备的输入,然后将处理过的数据转发到示波器,但我也有来自Pygame示波器实现的主循环。
当从logic.py初始化示波器时,如何防止程序控制流被卡在Pygame主循环中:
from oscilloscope import *
...
... #initializing filters to use, device to listen to etc.
...
self.osc = Oscilloscope(foo, bar) #creating an instance of an oscilloscope implemented with Pygame
self.osc.main() #calling the main loop of the oscilloscope. This handles all the drawing and updating screen.
#usually the flow would stop here as it is stuck in the Pygame main loop
#I need it to not get stuck so I can call the second main loop.
self.main() #captures and processes data from /dev/input/... sends processed data to the Pygame oscilloscope to draw it.由于没有任何实际的代码,我希望这些评论澄清了我想要做的事情。
发布于 2014-03-16 13:40:52
(我知道我的建议可能只是理论上的):
从self.osc.main()循环调用self.main()循环。要做到这一点,您必须编辑您的self.osc.main()函数,这样它就不会永远运行,而只能运行一次。保持self.main()循环不变,每个循环调用self.osc.main()一次。
而不是你现在正在做的事情,就像这样:
def main(): #This is the self.osc.main() function
while True: #run forever
doSomething()
def main(): #This is the self.main() function
while True: #also run forever
doSomethingElse()你可以这样做:
def main(): #This is the self.osc.main() function
doSomething() #notice I removed the loop. It only runs once. This is because the looping is handled by the other main() function (the one you call self.main())
def main(): #This is the self.main() function
while True: #also run forever
self.osc.main() #since we have now changed the self.osc.main() loop to run only once
doSomethingElse()我希望这能帮上忙。
https://stackoverflow.com/questions/22422711
复制相似问题