进程和线程锁用法一样:线程锁
import multiprocessing
def work(f, item, lock):
lock.acquire()
try:
with open(f, 'a+') as f:
f.write("a %s task\n" % (item))
except Exception as e:
print("产生异常...")
finally:
lock.release()
def main():
# 1). 实例化一个进程锁
lock = multiprocessing.Lock()
filename = 'doc/my1.log'
processes = []
for i in range(4):
p1 = multiprocessing.Process(target=work, args=(filename, i,lock))
p1.start()
processes.append(p1)
[process.join() for process in processes]
if __name__ == '__main__':
main()