标签:== lock get 多线程 nbsp 开发 code 接收 target
#加锁 ‘‘‘ 输出为 work_1 start work_2 start work_1 end work_2 end ‘‘‘ import time import multiprocessing def work_1(f,n,lock): print(‘work_1 start‘) lock.acquire() for i in range(n): with open(f,‘a‘) as fs: fs.write(‘i love pyhton \n‘) time.sleep(1) print(‘work_1 end‘) lock.release() def work_2(f,n,lock): print(‘work_2 start‘) lock.acquire() for i in range(n): with open(f,‘a‘) as fs: fs.write(‘come on baby \n‘) time.sleep(1) print(‘work_2 end‘) lock.release() if __name__ == ‘__main__‘: lock=multiprocessing.Lock() p1 = multiprocessing.Process(target=work_1,args = (‘file.txt‘,3,lock)) p2 = multiprocessing.Process(target=work_2, args=(‘file.txt‘, 1,lock)) p1.start() p2.start()
标签:== lock get 多线程 nbsp 开发 code 接收 target
原文地址:https://www.cnblogs.com/lanzhijie/p/12181945.html