multiprocessing控制对资源的访问

 

 

 

 1 import multiprocessing
 2 import sys
 3 
 4 
 5 def worker_with(lock, stream):      #
 6     with lock:
 7         stream.write('Lock acquired via with\n')
 8 
 9 
10 def worker_no_with(lock, stream):
11     lock.acquire()
12     try:
13         stream.write('Lock acquired directly\n')
14     finally:
15         lock.release()
16 
17 
18 lock = multiprocessing.Lock()
19 w = multiprocessing.Process(
20     target=worker_with,
21     args=(lock, sys.stdout),
22 )
23 nw = multiprocessing.Process(
24     target=worker_no_with,
25     args=(lock, sys.stdout),
26 )
27 
28 w.start()
29 nw.start()
30 
31 w.join()
32 nw.join()
控制对资源的访问
posted @ 2020-01-21 15:44  阿花007  阅读(86)  评论(0)    收藏  举报