当使用多进程对同一个文件进行操作时,为了避免冲突的发生,需要使用进程锁来对程序进行阻塞,以保证程序的正常运行。
from multiprocessing import Pool,Process
def demo01(pid,lock):
lock.acquire()
with open('file.txt',"a+", encoding='utf-8') as f:
f.write(pid)
...
...
lock.release()
lock = Lock()
for i in range(3):
i = str(i)
pool = Process(target=demo01,args=(i,lock))
pool.start()
错误加锁
lock = Lock()
pid = [(str(i), lock) for i in range(5)]
pool = Pool(5)
pool.map(demo01, pid)
pool.close()
pool.join()
像单进程这种将锁对象以参数传入的方法,如果直接应用于多进程中,会抛出一个异常
RuntimeError: Lock objects should only be shared between processes through inheritance
解决方案一:
在创建进程池对象时,传递Lock,将其变成一个对于面向所有子进程的全局对象
def demo01(pid, lock):
lock.acquire()
with open('file.txt', "a+", encoding='utf-8') as f:
print(pid)
f.write(str(pid))
...
...
lock.release()
def init(l):
global lock
lock = l
if __name__ == '__main__':
lock = Lock()
pid = [(str(i), lock) for i in range(5)]
pool = Pool(5,initializer=init,initargs=(lock,))
pool.map(demo01, pid)
pool.close()
pool.join()
解决方案二:
使用Manager()。