Python python 多进程,共享字典在子进程无法调用吗?

Ju-87C · 2021年09月01日 · 最后由 一枚老男孩 回复于 2021年09月07日 · 3179 次阅读

大佬们,我初学多进程,然后想法是,主进程、各个子进程共享一些数据,用于通信,子进程可以告知其余子进程自己的执行状态如何
于是使用了 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()

查了很多文章,还是没有找到比较满意的解决办法?
我多进程初学,是否有大佬见识过,指点一下小弟?

共收到 3 条回复 时间 点赞
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):
    for i in range(10):
        time.sleep(random.randint(1, 3))
        res = '包子%s' % i
        shared_dict[i] = i
        queue.put(res)
        print('\033[44m%s 生产了 %s\033[0m' % (os.getpid(), res))


if __name__ == '__main__':

    with Manager() as manager:
        share = manager.dict()
        q = Queue()
        print(share)
        # 生产者们:即厨师们
        p1 = Process(target=producer, args=(q, share))

        # 消费者们:即吃货们
        c1 = Process(target=consumer, args=(q, share))

        # 开始
        p1.start()
        c1.start()
        p1.join()
        c1.join()
        print('主')

try this!

解决了,绕了一条路,用 socket 来通信的~多谢

Ju-87C 回复

楼主可否发一下解决后的代码

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册