参考:
http://blog.csdn.net/moxiaomomo/article/details/11640439
http://www.cnblogs.com/guobaoyuan/p/6832470.html
#the output is:
#wait_for_event_timeout: starting
#wait_for_event: starting
#wait_for_event_timeout: e.is_set()->False
#main: event is set
#wait_for_event: e.is_set()->True
----------------------------------------------------
关于同步锁的使用:
1. 先设置一个进程间共享的锁,作为进程回调处理的入参
lock = multiprocessing.Lock()
proc1 = multiprocessing.Process(target=worker_with, args=(lock, f))
proc2 = multiprocessing.Process(target=worker_no_with, args=(lock, f))
2. 进程对共享资源操作的时候,先获取锁,然后操作共享变量,然后再释放锁
lock.acquire() #获取公共锁,如果锁被占用,则阻塞在这里,等待锁的释放
# do somthing, 比如打开写入文本,修改共享内存中的变量等。。
......
lock.release() #释放公共锁
网友评论