从芹菜任务使用多处理池引发异常



对于那些阅读本文的人: 我决定改用 RQ,它在运行使用多处理模块的代码时不会失败。我建议你使用它。

我正在尝试使用 Python 3 和 redis 作为代理(在 Mac 上运行它)从芹菜任务中使用多处理池。但是,我似乎甚至无法从 Celery 任务中创建多处理池对象!相反,我得到了一个奇怪的异常,我真的不知道该怎么办。

谁能告诉我如何做到这一点?

任务:

from celery import Celery
from multiprocessing.pool import Pool
app = Celery('tasks', backend='redis', broker='redis://localhost:6379/0')
@app.task
def test_pool():
    with Pool() as pool:
        # perform some task using the pool
        pool.close()
    return 'Done!'

我使用以下方法添加到芹菜中:

celery -A tasks worker --loglevel=info

然后通过以下 Python 脚本运行它:

import tasks
tasks.test_pool.delay()

返回以下芹菜输出:

[2015-01-12 15:08:57,571: INFO/MainProcess] Connected to redis://localhost:6379/0
[2015-01-12 15:08:57,583: INFO/MainProcess] mingle: searching for neighbors
[2015-01-12 15:08:58,588: INFO/MainProcess] mingle: all alone
[2015-01-12 15:08:58,598: WARNING/MainProcess] celery@Simons-MacBook-Pro.local ready.
[2015-01-12 15:09:02,425: INFO/MainProcess] Received task: tasks.test_pool[38cab553-3a01-4512-8f94-174743b05369]
[2015-01-12 15:09:02,436: ERROR/MainProcess] Task tasks.test_pool[38cab553-3a01-4512-8f94-174743b05369] raised unexpected: AttributeError("'Worker' object has no attribute '_config'",)
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/site-packages/celery/app/trace.py", line 240, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/celery/app/trace.py", line 438, in __protected_call__
    return self.run(*args, **kwargs)
  File "/Users/simongray/Code/etilbudsavis/offer-sniffer/tasks.py", line 17, in test_pool
    with Pool() as pool:
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 150, in __init__
    self._setup_queues()
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 243, in _setup_queues
    self._inqueue = self._ctx.SimpleQueue()
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/context.py", line 111, in SimpleQueue
    return SimpleQueue(ctx=self.get_context())
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/queues.py", line 336, in __init__
    self._rlock = ctx.Lock()
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/context.py", line 66, in Lock
    return Lock(ctx=self.get_context())
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/synchronize.py", line 163, in __init__
    SemLock.__init__(self, SEMAPHORE, 1, 1, ctx=ctx)
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/synchronize.py", line 59, in __init__
    kind, value, maxvalue, self._make_name(),
  File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/synchronize.py", line 117, in _make_name
    return '%s-%s' % (process.current_process()._config['semprefix'],
AttributeError: 'Worker' object has no attribute '_config'

这是芹菜的已知问题。它源于台球依赖中引入的问题。解决方法是手动设置当前进程的_config属性。感谢用户@martinth在下面的解决方法。

from celery.signals import worker_process_init
from multiprocessing import current_process
@worker_process_init.connect
def fix_multiprocessing(**kwargs):
    try:
        current_process()._config
    except AttributeError:
        current_process()._config = {'semprefix': '/mp'}

worker_process_init挂钩将在工作进程初始化时执行代码。我们只需检查_config是否存在,如果不存在,则进行设置。

通过 Edy 评论中链接到的 Celery 问题报告中的有用评论,我能够通过导入 billiard 模块的 Pool 类来解决这个问题。

取代

from multiprocessing import Pool

from billiard.pool import Pool

一个快速的解决方案是在实现中使用基于线程的"虚拟"multiprocessing。改变

from multiprocessing import Pool  # or whatever you're using

from multiprocessing.dummy import Pool

但是,由于这种并行性是基于线程的,因此通常的警告 (GIL) 适用。

相关内容

  • 没有找到相关文章

最新更新