具有多处理功能的python队列,出现腌制错误

时间:2019-06-03 13:49:06

标签: python multiprocessing queue

为什么此代码会向我抛出错误:

import multiprocessing as mp


def my_process(q):
    print(q.get())


if __name__ == '__main__':
    q = mp.Queue(maxsize=4)

    iolock = mp.Lock()
    pool = mp.Pool(processes=2, initializer=my_process, initargs=(q))

    for x in range(0,10):
        q.put(x)

    pool.close()
    pool.join()


AttributeError: Can't get attribute 'my_process' on <module '__main__' (built-in)>

我了解这与我要多进程处理的函数的Python酸洗有关,但是如何解决呢?

在定义池之前,我已经定义了函数my_process

编辑: python 3.7.3,ipython 7.0.1

AttributeError: Can't get attribute 'my_process' on <module '__main__' (built-in)>
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\PROGRAMS\Miniconda3_64\envs\py37hdb\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\PROGRAMS\Miniconda3_64\envs\py37hdb\lib\multiprocessing\spawn.py", line 115, in _main
    self = reduction.pickle.load(from_parent)

0 个答案:

没有答案