我是multiprocessing
模块的新手,正试图了解我要去哪里。几天前,我也有类似的代码工作,但是不幸的是丢失了它。这是我所拥有的:
import pdb
import time
import subprocess
import queue
import multiprocessing
import numpy as np
data_queue = multiprocessing.Queue()
def launchJob():
import numpy as np
print("launching job")
data_queue.put(np.array([1, 2, 3]))
print("done with job")
def launchJob2():
print("launching job")
data_queue.put("hi!!!!")
print("done with job")
class ProcessMgr():
def __init__(self):
self.idx = 0
self.z = []
self.num_mgrs = 4
for i in range(self.num_mgrs):
multiprocessing.Process(target = launchJob2)
print("launched job %d"%i)
def getBatch(self):
while True:
if len(self.z) > 0:
return self.z.pop(0)
else:
try:
print("trying to get data from queue")
d = data_queue.get()
return d
except:
print("still waiting")
time.sleep(5)
print("done waiting for this iteration")
m = ProcessMgr()
print(m.getBatch())
print(m.getBatch())
pdb.set_trace()
当我在命令行上运行它时,这就是我得到的:
>> python3 async3.py
launched job 0
launched job 1
launched job 2
launched job 3
trying to get data from queue
然后什么也没有发生。多数情况下就是这样(我已经尝试了8次)。一次,当我将其放置足够长时间而没有键盘中断时,出现以下错误:
IndexError: pop from empty list
从一行来
return self.z.pop(0)
这很奇怪,因为既然我从没有在z上放任何东西,我怎么会超过长度的校验?这是我尝试过的:
我会很乐意告诉我我为解决这个问题所做的愚蠢工作。非常感谢。