from multiprocessing import Process,Lock
import time
def get():
for i in range(3):
time.sleep(1)
print(i)
def task(lock):
# lock.acquire() #获取锁
get()
# lock.release() #释放锁
if __name__ == '__main__':
lock=Lock()
for i in range(5): #并发5个
p=Process(target=task,args=(lock,))
p.start()
二、加锁
from multiprocessing import Process,Lock
import time
def get():
for i in range(3):
time.sleep(1)
print(i)
def task(lock):
lock.acquire() #获取锁
get()
lock.release() #释放锁
if __name__ == '__main__':
lock=Lock()
for i in range(5): #并发5个
p=Process(target=task,args=(lock,))
p.start()