在我在其他代码中实现之前,我已经编写了一些代码来测试多重处理。主代码向另一个程序(在另一个内核上运行)发送一个数字。该程序递增并返回数字。主代码递增并再次返回值。所有的工作都很好,但是主程序中的循环需要一段时间<;超时窗体,从不退出。对我来说似乎很简单,但它从来就不存在循环。我想知道当没有返回值(.get())时它是否挂起,但我尝试了“try:”,并使超时非常短,循环非常大。有什么建议吗
该代码在Windows7上运行,最终将在Raspberry Pi2上运行
主程序
import multiprocessing as mp
import multi_processing_slave as MPS
from time import perf_counter as TimeIs
if __name__ == "__main__":
print("Hello World")
mp.set_start_method("spawn")
q = mp.Queue()
r = mp.Queue()
p = mp.Process(target = MPS.foo, args = (q, r))
p.start()
ThisVar = 0
Timer = TimeIs() + 2
while TimeIs() < Timer - 1: pass
print("time remaining is", Timer - TimeIs())
while TimeIs() < Timer:
#try:
r.put(ThisVar)
#except: pass
#try:
ThisVar = int(q.get()) + 1
#except:
#r.put(ThisVar)
print("master ThisVar", ThisVar, "and time remaining is", round(Timer - TimeIs(), 4))
#p.join()
#p.close()
p.terminate()
print("at end, ThisVar is", ThisVar, "and", Timer - TimeIs(), "seconds remaining")
名为multi\ u processing\ u Slave的从程序
def foo(q, r):
for i in range(100):
ThisVar2 = r.get() + 1
q.put(ThisVar2)
print("foo value", ThisVar2)
print("foo has finished")
return
从属进程退出后,主进程再执行一次
r.put()
,然后一直等待q.get()
返回。您可以通过向q.get()
提供超时值(以秒为单位)来解决此问题:请注意,
q.get()
上的超时将引发Empty
异常你如何自己发现这个问题:
添加显示程序进度和状态的调试打印语句
学习使用调试器
相关问题 更多 >
编程相关推荐