简体   繁体   中英

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. 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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