繁体   English   中英

Python:在进程池中使用多处理管理器

[英]Python:using multiprocessing manager in process pool

我使用multiprocessing.managers.BaseManager在服务器端管理一个队列,我尝试在另一个使用进程池的python脚本中使用此队列,但是我总是收到如下错误消息,并且此python脚本的主要代码是如下,它将以run()方法开始。

File "/usr/lib/python2.7/multiprocessing/connection.py", line 435, in answer_challenge
    raise AuthenticationError('digest sent was rejected')
AuthenticationError: (AuthenticationError('digest sent was rejected',), <function RebuildProxy at 0x7ff8de0b8320>, (<function AutoProxy at 0x7ff8de0b7938>, Token(typeid='Queue', address=('localhost', 12345), id='7f4624039cd0'), 'pickle', {'exposed': ('cancel_join_thread', 'close', 'empty', 'full', 'get', 'get_nowait', 'join_thread', 'put', 'put_nowait', 'qsize')}))


def __init__(self, spider_count=cpu_count()):
    self._spider_count = spider_count
    mgr = MyManager(address=('localhost', 12345), authkey='xxxxx')
    server = mgr.connect()
    self._queue = mgr.Queue()

def run(self):
        pool = Pool(self._spider_count)
        while not self._queue.empty():
            #add some control on q.get() if queue is empty
            pool.apply_async(self.startCrawl, (self._queue.get(),))
        pool.close()
        pool.join()

但是,当我在单线程中使用它时,它运行良好,当使用池时,会出现此错误消息。

这听起来像http://bugs.python.org/issue7503中描述的问题

实际上,使用管理器的所有进程都应将current_process().authkey设置为相同的值。

解决方法是在__init__分配:

multiprocessing.current_process().authkey = 'xxxxx'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM