python-Lock进程同步解决互斥

 1  #!/usr/bin/python
 2 from multiprocessing import Process,Lock
 3 import time,sys
 4 
 5 def A(lock):
 6     with lock:
 7         for i in range(10):
 8             time.sleep(2)
 9             sys.stdout.write("hello world
")
10 
11 def B(lock):
12     lock.acquire()
13     try:
14         for i in range(10):
15             time.sleep(2)
16             sys.stdout.write("  nihao  a 
")
17     finally:
18         lock.release()
19 
20 lock=Lock()
21 m=Process(target=A,args=(lock,))
22 n=Process(target=B,args=(lock,))
23 
24 m.start()
25 n.start()
26 
27 m.join()
28 n.join()
原文地址:https://www.cnblogs.com/chengyunshen/p/7195938.html