Django Celery 接收和接受任务,但不执行它们

hal*_*nes 5 django redis celery django-celery ubuntu-16.04

我正在运行 Ubuntu 16.04、Gunicorn、Nginx 的 Digital Ocean 上运行 Django 1.11(使用 Cookiecutter-Django 模板构建)服务器,并尝试使用 Redis 设置 Celery 任务。当我这样做时,该服务似乎可以正常工作并接收定期任务:

celery -A config worker -B -l debug
Run Code Online (Sandbox Code Playgroud)

任务被接收并接受,但它们不执行。为了测试,我发送了这个函数:

@shared_task(name="sum_two_numbers")
def add(x, y, **kwargs):
    return x + y
Run Code Online (Sandbox Code Playgroud)

和:

add.delay(1,3)
Run Code Online (Sandbox Code Playgroud)

这是运行 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
Run Code Online (Sandbox Code Playgroud)

当我在本地运行时,它工作得很好。我在这里做错了什么?

Mob*_*iya 7

您可以尝试使用此命令 -

celery -A <App_name> worker -l info --without-gossip --without-mingle --without-heartbeat -Ofair --pool=solo
Run Code Online (Sandbox Code Playgroud)

这通过汇集独奏情绪解决了我的问题。也许这也是你的情况。