我的多进程设置或多或少看起来如下:
import multiprocessing
class Worker(multiprocessing.Process):
def __init__(self, name):
self.name = name
def run(self):
while True:
#do something
print( self.name + " is working!")
#wait a few seconds
def main():
workers = []
for a in ["A", "B", "C", "D"]:
w = Worker(a)
workers += [w]
w.start()
我想要做的是能够从main()更改worker的名称。为每个工人创建一个单独的队列是唯一的方法,还是有更好的东西?我觉得我通过使用多进程不断遇到限制,我不知道我不能再做什么了。
答案 0 :(得分:1)
你可以是Pipe
:
import multiprocessing
class Worker(multiprocessing.Process):
def __init__(self, name):
self.name = name
self.change_name_r, self.change_name_w = multiprocessing.Pipe(False)
def run(self):
while True:
#do something
if self.change_name_r.poll():
self.name = self.change_name_r.recv()
print( self.name + " is working!")
#wait a few seconds
def change_name(self, new_name):
self.change_name_w.send(new_name)