在python中建立两个进程之间的通信的最佳方式是什么?一些google搜索后,我试着这样做:Python中的进程通信
parent_pipe, child_pipe = Pipe()
p = Process(target = instance_tuple.instance.run(), \
args = (parent_pipe, child_pipe,))
p.start()
将数据发送到子进程:
command = Command(command_name, args)
parent_pipe.send(command)
过程的目标函数:
while True:
if (self.parent_pipe.poll()):
command = parent_pipe.recv()
if (command.name == 'init_model'):
self.init_model()
elif (command.name == 'get_tree'):
tree = self.get_fidesys_tree(*command.args)
result = CommandResult(command.name, tree)
self.child_pipe.send(result)
elif(command.name == 'set_variable'):
name = command.args[0]
value = command.args[1]
self.config[name] = value
但它似乎不工作(子进程通过parent_pipe
没有收到任何东西)。我该如何解决它?
在此先感谢。
检查parent_pipe。它是否被实例化? – krs1
@ krs1是的,无论是在子进程还是父进程中,管道都被实例化。 –