我对celery具有以下实现,这不是Django项目
项目结构
proj
|
proj
├── celery.py
├── conf.py
├── __init__.py
├── run.py
└── tasks.py
conf.py
broker_url = "redis://localhost:6379/0"
result_backend = "redis://localhost:6379/0"
task_serializer = "json"
timezone = "Europe/London"
enable_utc = True
include = ['proj.tasks']
beat_schedule = {
'add-every-2-seconds': {
'task': 'proj.tasks.test',
'schedule': 2.0,
'args': (16, 16)
},
}
run.py
from proj.tasks import add
res = add.delay(2, 2)
print(res.get())
tasks.py
from proj.celery import app
@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
# Calls test('hello') every 10 seconds.
sender.add_periodic_task(2.0, test.s('hello'), name='add every 10')
# Calls test('world') every 30 seconds
sender.add_periodic_task(3.0, test.s('world'), expires=10)
@app.task
def test(arg):
print(arg)
@app.task
def add(x, y):
return x + y
@app.task
def mul(x, y):
return x * y
@app.task
def xsum(numbers):
return sum(numbers)
遵循教程celery.py
from __future__ import absolute_import, unicode_literals
from celery import Celery
from proj import conf
app = Celery('proj')
app.config_from_object(conf)
# Optional configuration, see the application user guide.
app.conf.update(
result_expires=3600,
)
if __name__ == '__main__':
app.start()
proj celery -A proj worker -l info
尽管有效
运行run.py时出现错误
from celery import Celery
ImportError: cannot import name 'Celery'
将celery.py重命名为另一个名称会导致
proj celery -A proj worker -l info
Error:
Unable to load celery application.
Module 'proj' has no attribute 'celery'
如何解决此问题?