大佬们,我初学多进程,然后想法是,主进程、各个子进程共享一些数据,用于通信,子进程可以告知其余子进程自己的执行状态如何
于是使用了 multiprocessing.Manager() 库,他里面有 dict() 感觉挺好用的
但是遇到了问题,下面是我的举例代码:
import os
import random
import time
from multiprocessing import Process, Queue, Manager
def consumer(queue, shared_dict: dict):
while True:
res = queue.get()
time.sleep(random.randint(1, 3))
print(shared_dict.keys())
print('\033[43m%s 吃 %s\033[0m' % (os.getpid(), res))
def producer(queue, shared_dict: dict):
for i in range(10):
time.sleep(random.randint(1, 3))
res = '包子%s' % i
shared_dict.__setitem__(i, i)
queue.put(res)
print('\033[44m%s 生产了 %s\033[0m' % (os.getpid(), res))
if __name__ == '__main__':
q = Queue()
with Manager() as manager:
share = manager.dict()
print(share)
# 生产者们:即厨师们
p1 = Process(target=producer, args=(q, share))
# 消费者们:即吃货们
c1 = Process(target=consumer, args=(q, share))
# 开始
p1.start()
c1.start()
print('主')
我只是想在厨师那里随便往 shared_dict 里面存点键值对,然后试试能否在吃货那边查看到,但是呢,生产者存的时候就报错了:
Process Process-2:
Traceback (most recent call last):
File "D:\python\lib\multiprocessing\managers.py", line 827, in _callmethod
conn = self._tls.connection
AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\python\lib\multiprocessing\process.py", line 315, in _bootstrap
self.run()
File "D:\python\lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "D:\PycharmProjects\pythonLearn\pc_multiprocess.py", line 19, in producer
shared_dict.__setitem__(i, i)
File "<string>", line 2, in __setitem__
File "D:\python\lib\multiprocessing\managers.py", line 831, in _callmethod
self._connect()
查了很多文章,还是没有找到比较满意的解决办法?
我多进程初学,是否有大佬见识过,指点一下小弟?