![](/img/trans.png)
[英]In python multiprocessing,queue.put will block subprocess
[英]Python: multiprocessing Queue.put() in module won't send anything to parent process
我正在尝试使用 Python 中的多处理 package,更准确地说是 Queue() class,使 2 个进程相互通信。 从父进程中,我想每 5 秒获取一次子进程的更新值。 这个子进程是 class function。 我做了一个玩具示例,一切正常。
但是,当我尝试在我的项目中实现此解决方案时,似乎子模块中子进程的 Queue.put() 方法不会向父进程发送任何内容,因为父进程不会打印所需的值和代码永远不会停止运行。 实际上,父进程只打印发送给子进程的值,这里是True
,但正如我所说,永远不会停止。
所以我的问题是:
我的玩具示例中有任何错误吗?
我应该如何修改我的项目才能让它像我的玩具示例一样工作?
玩具示例:作品
主模块
from multiprocessing import Process, Event, Lock, Queue, Pipe
import time
import test_mod as test
def loop(output):
stop_event = Event()
q = Queue()
child_process = Process(target=test.child.sub, args=(q,))
child_process.start()
i = 0
print("started at {} ".format(time.time()))
while not stop_event.is_set():
i+=1
time.sleep(5)
q.put(True)
print(q.get())
if i == 5:
child_process.terminate()
stop_event.set()
output.put("main process looped")
if __name__ == '__main__':
stop_event, output = Event(), Queue()
k = 0
while k < 5:
loop_process = Process(target=loop, args=(output,))
loop_process.start()
print(output.get())
loop_process.join()
k+=1
子模块
from multiprocessing import Process, Event, Lock, Queue, Pipe
import time
class child(object):
def __init__(self):
pass
def sub(q):
i = 0
while i < 2000:
latest_value = time.time()
accord = q.get()
if accord == True:
q.put(latest_value)
accord = False
time.sleep(0.0000000005)
i+=1
项目代码:不起作用
主模块
import neat #package in which the submodule is
import *some other stuff*
def run(config_file):
config = neat.Config(some configuration)
p = neat.Population(config)
**WHERE MY PROBLEM IS**
stop_event = Event()
q = Queue()
pe = neat.ParallelEvaluator(**args)
child_process = Process(target=p.run, args=(pe.evaluate, q, other args))
child_process.start()
i = 0
while not stop_event.is_set():
q.put(True)
print(q.get())
time.sleep(5)
i += 1
if i == 5:
child_process.terminate()
stop_event.set()
if __name__ == '__main__':
run(config_file)
子模块
class Population(object):
def __init__():
*initialization*
def run(self, q, other args):
while n is None or k < n:
*some stuff*
accord = add_2.get()
if accord == True:
add_2.put(self.best_genome.fitness)
accord = False
return self.best_genome
注意:
我不习惯多处理
考虑到整个代码太长,我试图给出我项目中最相关的部分。
我也考虑过使用 Pipe(),但是这个选项也不起作用。
如果我没看错,您想要的子模块是 class Population
。 但是,您使用ParallelEvaluator
类型的参数开始您的过程。 接下来,我看不到您将 Queue q
提供给子流程。 这就是我从提供的代码中看到的:
stop_event = Event()
q = Queue()
pe = neat.ParallelEvaluator(**args)
child_process = Process(target=p.run, args=(pe.evaluate, **args)
child_process.start()
此外,以下几行创建了一个竞争条件:
q.put(True)
print(q.get())
get
命令就像一个pop
。 所以它需要一个元素并将其从队列中删除。 如果您的子进程没有访问这两行之间的队列(因为它很忙),那么True
将永远不会进入子进程。 因此,最好两个使用多个队列。 每个方向一个。 就像是:
stop_event = Event()
q_in = Queue()
q_out = Queue()
pe = neat.ParallelEvaluator(**args)
child_process = Process(target=p.run, args=(pe.evaluate, **args))
child_process.start()
i = 0
while not stop_event.is_set():
q_in.put(True)
print(q_out.get())
time.sleep(5)
i += 1
if i == 5:
child_process.terminate()
stop_event.set()
这是你的子模块
class Population(object):
def __init__():
*initialization*
def run(self, **args):
while n is None or k < n:
*some stuff*
accord = add_2.get() # add_2 = q_in
if accord == True:
add_3.put(self.best_genome.fitness) #add_3 = q_out
accord = False
return self.best_genome
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.