我尝试使用redis后端在Windows上运行Celery示例。代码如下:
from celery import Celery
app = Celery('risktools.distributed.celery_tasks',
backend='redis://localhost',
broker='redis://localhost')
@app.task(ignore_result=False)
def add(x, y):
return x + y
@app.task(ignore_result=False)
def add_2(x, y):
return x + y
我使用iPython控制台启动任务:
>>> result_1 = add.delay(1, 2)
>>> result_1.state
'PENDING'
>>> result_2 = add_2.delay(2, 3)
>>> result_2.state
'PENDING'
似乎两个任务都没有执行,但Celery worker输出显示它们成功了:
[2014-12-08 15:00:09,262: INFO/MainProcess] Received task: risktools.distributed.celery_tasks.add[01dedca1-2db2-48df-a4d6-2f06fe285e45]
[2014-12-08 15:00:09,267: INFO/MainProcess] Task celery_tasks.add[01dedca1-2db2-48df-a4d6-2f06fe28
5e45] succeeded in 0.0019998550415s: 3
[2014-12-08 15:00:24,219: INFO/MainProcess] Received task: risktools.distributed.celery_tasks.add[cb5505ce-cf93-4f5e-aebb-9b2d98a11320]
[2014-12-08 15:00:24,230: INFO/MainProcess] Task celery_tasks.add[cb5505ce-cf93-4f5e-aebb-9b2d98a1
1320] succeeded in 0.010999917984s: 5
我已尝试根据Celery documentation对此问题进行问题排查,但没有一条建议有用。我做错了什么,如何从Celery任务中获得结果?
UPD:
我添加了一个没有ignore_result
参数的任务,但没有任何改变
@app.task
def add_3(x, y):
return x + y
>>>r = add_3.delay(2, 2)
>>>r.state
'PENDING'
答案 0 :(得分:9)
根据Celery 'Getting Started' not able to retrieve results; always pending和https://github.com/celery/celery/issues/2146,这是一个Windows问题。 Celery --pool=solo
选项解决了这个问题。
答案 1 :(得分:2)
在Windows上尝试使用Celery --pool=solo
代替-P threads
。
答案 2 :(得分:1)
谢谢大家。
我的芹菜配置:
-------------- celery@DESKTOP-FD38GOO v4.4.2 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2020-04-17 06:58:18
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: mysite:0x25cfd40d208
- ** ---------- .> transport: redis://localhost:6379//
- ** ---------- .> results: redis://localhost:6379/1
- *** --- * --- .> concurrency: 8 (thread)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. mysite.celery.debug_task
. supplier.tasks.add
. supplier.tasks.count_widgets
. supplier.tasks.count_widgets2
. supplier.tasks.mul
. supplier.tasks.xsum
我已解决以下问题:
我大约等待了1天这种问题,然后尝试卸载redis并在Windows 10上安装redis几次。
最后我发现没有并发配置。
第一个解决方案:
芹菜-mysite工作者-l信息-P线程
第二种解决方法:
芹菜-我的网站工作者-l信息--pool = solo
我的芹菜配置:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_IGNORE_RESULT = False
CELERY_TIMEZONE = TIME_ZONE
CELERY_TRACK_STARTED = True
CELERYD_LOG_FILE = os.path.join(
BASE_DIR, 'celery', 'logs')
CELERYD_LOG_LEVEL = "INFO"
答案 3 :(得分:0)
从芹菜文档中删除ignore_result=False
Task.ignore_result
Don’t store task state. Note that this means you can’t
use AsyncResult to check if the task is ready, or get its return value.
答案 4 :(得分:0)
设置CELERY_TASK_TRACK_STARTED = True
(或track_started = True单独完成任务)也有帮助-这将启用STARTED状态。