简体   繁体   English

Python在父进程和子进程之间共享一个带有Event()变量的字典

[英]Python sharing a dict with Event() variables inside it between parent and child processes

I want to share a dict with some Event() variables inside it between parent and child processes but don't know how. 我想在父进程和子进程之间与其中的一些Event()变量共享字典,但不知道如何做。 Notice that I need to add or delete (key, value) after the process is started. 请注意,启动过程后,我需要添加或删除(键,值)。 It seems that neither a normal dict nor a multiprocessing.Manager.dict like below can satisfy my requirements. 看起来像下面这样的普通字典或multiprocessing.Manager.dict都不能满足我的要求。

import time
from multiprocessing import Process, Event, Manager

manager = Manager()
d1 = manager.dict()
#  d1 = dict()

def fun():
    # do something and then set d1[1]
    time.sleep(4)
    d1[1].set()

p = Process(target=fun, name='fun')
p.start()
d1[1] = Event() # add a (key, value) after the process is defined
p.join()
print 'After fun, d1[1] = %s' % (d1[1].is_set())

So what is the right way to share these semaphores between parent and child processes? 那么在父进程和子进程之间共享这些信号量的正确方法是什么?

Replace 更换

d1[1] = Event()

with

d1[1] = manager.Event()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM