我有以下文件夹:
proj
├── __init__.py
├── celery.py
├── tasks.py
├── foo_launcher.py
首先,我必须启动 celery worker。
celery -A proj worker -l info
celery.py 代码:
from __future__ import absolute_import, unicode_literals
from celery import Celery
app = Celery('project', broker='amqp://guest@localhost//', include=['project.tasks'])
if __name__ == '__main__':
app.start()
Celery worker 工作正常,但是当我要启动时foo_launcher.py,我无法使用这些任务,因为foo_launcher.py取决于tasks.py和tasks.py取决于celery.py. 这是问题所在。我需要导入 celery.py 文件而不是 celery 库。
所以,这是 tasks.py
from .celery import app #Here Import celery.py not celery library
@app.task
def add(x, y):
return x + y
和这里 foo_launcher.py
from tasks import add
result = add.apply_async((2,2), queue='lopri',coutdown=10)
print(result)
显然一切都有意义,但是当我尝试启动时foo_launcher.py:
python foo_launcher.py
它向我发起了以下错误:
Traceback (most recent call last):
File ".\foo_launcher.py", line 1, in <module>
from tasks import add
File "C:\Users\beca4\Desktop\proj\tasks.py", line 1, in <module>
from .celery import app
ImportError: attempted relative import with no known parent package
我不知道我能做什么。有任何想法吗?
相关分类