芹菜与ProcessPoolExecutor返回ModuleNotFoundError

时间:2019-08-28 07:58:07

标签: python-3.x celery concurrent.futures

我正在使用celery和current.futures,但是程序返回ModuleNotFoundError。

我使用ProcessPoolExecutor但失败了,但是当我将其更改为ThreadPoolExecutor时,看起来还可以。

这是我的测试程序的结构:

test/
  _celery.py
  main.py
  lib/
    task/
        test.py

_celery.py:

from celery import Celery
from lib.task import test

app = Celery(broker_url='pyamqp://guest@localhost//', 
celery_result_backend='pyamqp://guest@localhost//')

@app.task
def test_(word_list):
    test.test(word_list)

lib / task / test.py:

import concurrent.futures


def process(word):
    return word


def test(word_list):
    with concurrent.futures.ProcessPoolExecutor() as executor:
        future_process = {executor.submit(process, word): word for word in word_list}

main.py:

from _celery import test_

if __name__ == '__main__':
    test_.delay(['test1', 'test2', 'test3'])

然后我使用以下命令启动芹菜服务:

celery -A _celery worker -l info --pool=solo

实际上,终端返回:

[2019-08-28 15:30:05,116: INFO/MainProcess] Received task: _celery.test_[d8e4d74d-4c32-44a8-a468-eeed92079258]
Process SpawnProcess-9:
Traceback (most recent call last):
  File "c:\users\wind\appdata\local\programs\python\python37\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "c:\users\wind\appdata\local\programs\python\python37\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "c:\users\wind\appdata\local\programs\python\python37\lib\concurrent\futures\process.py", line 233, in _process_worker
    call_item = call_queue.get(block=True)
  File "c:\users\wind\appdata\local\programs\python\python37\lib\multiprocessing\queues.py", line 113, in get
    return _ForkingPickler.loads(res)
ModuleNotFoundError: No module named 'lib'
[2019-08-28 15:30:06,711: INFO/MainProcess] Task _celery.test_[d8e4d74d-4c32-44a8-a468-eeed92079258] succeeded in 1.5940000000009604s: None

如果我不使用celery并直接在main.py中调用test.test(),那就可以了,我很困惑。

我正在网上搜索很长时间,但没有用。请帮助我或尝试给我一些解决方法的想法。

0 个答案:

没有答案