【发布时间】:2018-07-30 09:01:45
【问题描述】:
我在运行 Ubuntu 16.04、Gunicorn、Nginx 的 Digital Ocean 上运行 Django 1.11(使用 Cookiecutter-Django 模板构建)服务器,并尝试使用 Redis 设置 Celery 任务。当我这样做时,该服务似乎可以正常工作并接收定期任务:
celery -A config worker -B -l debug
并且任务被接收并接受,但它们不执行。为了测试,我发送了这个函数:
@shared_task(name="sum_two_numbers")
def add(x, y, **kwargs):
return x + y
与:
add.delay(1,3)
这是运行 Celery 的控制台的完整打印输出:
-------------- celery@myproject v4.1.0 (latentcall)
---- **** -----
--- * *** * -- Linux-4.4.0-112-generic-x86_64-with-Ubuntu-16.04-xenial 2018-02-19 23:18:12
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: myproject:0x7f2cd60dc9e8
- ** ---------- .> transport: redis://127.0.0.1:6379//
- ** ---------- .> results: redis://localhost:6379/
- *** --- * --- .> concurrency: 1 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. . .
. sum_two_numbers
[2018-02-19 23:18:12,858: INFO/MainProcess] Connected to redis://127.0.0.1:6379//
[2018-02-19 23:18:12,876: INFO/MainProcess] mingle: searching for neighbors
[2018-02-19 23:18:13,910: INFO/MainProcess] mingle: all alone
[2018-02-19 23:18:13,924: WARNING/MainProcess] /home/user/.virtualenvs/myproject/lib/python3.5/site-packages/celery/fixups/django.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2018-02-19 23:19:38,714: INFO/MainProcess] Received task: sum_two_numbers[ab5b5547-1337-4dec-8848-c15e1a194b51]
[2018-02-19 23:19:38,715: DEBUG/MainProcess] TaskPool: Apply <function _fast_trace_task at 0x7f2cd5fce510> (args:('sum_two_numbers', 'ab5b5547-1337-4dec-8848-c15e1a194b51', {'root_id': 'ab5b5547-1337-4dec-8848-c15e1a194b51', 'task': 'sum_two_numbers', 'group': None, 'correlation_id': 'ab5b5547-1337-4dec-8848-c15e1a194b51', 'id': 'ab5b5547-1337-4dec-8848-c15e1a194b51', 'timelimit': [None, None], 'expires': None, 'retries': 0, 'argsrepr': '(1, 3)', 'eta': None, 'origin': 'gen23535@myproject', 'reply_to': 'e67c54ef-3c66-3720-9e1f-62ef3d76882d', 'kwargsrepr': '{}', 'lang': 'py', 'parent_id': None, 'delivery_info': {'priority': 0, 'redelivered': None, 'routing_key': 'celery', 'exchange': ''}}, b'[[1, 3], {}, {"errbacks": null, "chain": null, "chord": null, "callbacks": null}]', 'application/json', 'utf-8') kwargs:{})
[2018-02-19 23:19:38,722: DEBUG/MainProcess] Task accepted: sum_two_numbers[ab5b5547-1337-4dec-8848-c15e1a194b51] pid:23512
当我在本地运行时,它工作得很好。我在这里做错了什么?
【问题讨论】:
-
日志显示任务运行成功。
-
不完全是,在我的本地机器上运行它时,我也得到了这个:
[2018-02-20 00:40:09,626: INFO/MainProcess] Task sum_two_numbers[f5d2d8b6-ebc6-492c-9b34-4c73fd46e134] succeeded in 0.0s: 4 -
Task sum_two_numbers[f5d2d8b6-ebc6-492c-9b34-4c73fd46e134] succeeded in 0.0s:这说明它运行成功了! -
当您再次调用
add.delay(1,3)时,您将再次看到相同的日志,这意味着该任务正在运行。 -
任务运行成功添加打印状态以检查正确的结果
标签: django redis celery ubuntu-16.04 django-celery