码迷,mamicode.com
首页 > 其他好文 > 详细

multiprocessing控制对资源的访问

时间:2020-01-21 16:14:21      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:worker   技术   directly   控制   F12   start   targe   isp   join()   

 

 

 

技术图片
 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()
控制对资源的访问

multiprocessing控制对资源的访问

标签:worker   技术   directly   控制   F12   start   targe   isp   join()   

原文地址:https://www.cnblogs.com/landihua/p/12221977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!