python3.7環(huán)境下挪略,安裝了Celery==4.2.0历帚,在啟動celery任務(wù)的時候废酷,報錯
根據(jù)Celery官方* Getting Started ?教程,首先創(chuàng)建了一個簡單的tasks任務(wù):
文件名 tasks.py
# coding: utf-8
from celery import Celery
broker = 'redis://127.0.0.1:6379/0'
backend = 'redis://127.0.0.1:6379/0'
app = Celery('tasks', broker=broker, backend=backend)
@app.task
def add(x, y):
return x + y
在文件目錄下抹缕,命令終端啟動Celery澈蟆,生產(chǎn)環(huán)境可用supervisor管理,這里只是簡單演示卓研,命令如下
celery -A tasks worker --loglevel=info
第一次我運行的時候報錯了趴俘,如下:
File "/Users/li/.venv/venv-myprojet/lib/python3.7/site-packages/celery/backends/redis.py", line 22
from . import async, base
^
SyntaxError: invalid syntax
搜索了一下錯誤原因,原來是async名稱更換了奏赘,如下
[Rename `async` to `asynchronous` (async is a reserved keyword in Python 3.7) #4879](https://github.com/celery/celery/pull/4879)
開發(fā)人員已經(jīng)處理了這個issue寥闪,合并了master,快速的解決方案是通過github安裝celery磨淌,命令如下:
pip install --upgrade https://github.com/celery/celery/tarball/master
再次運次疲憋,那個應(yīng)該可以看到如下正常輸出:
-------------- celery@lideMacBook-Pro.local v4.2.0 (windowlicker)
---- **** -----
--- * *** * -- Darwin-16.7.0-x86_64-i386-64bit 2018-07-16 22:50:59
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: tasksex:0x1110f3a58
- ** ---------- .> transport: redis://127.0.0.1:6379/0
- ** ---------- .> results: redis://127.0.0.1:6379/0
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. tasks.add
[2018-07-16 22:50:59,543: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[2018-07-16 22:50:59,551: INFO/MainProcess] mingle: searching for neighbors
[2018-07-16 22:51:00,571: INFO/MainProcess] mingle: all alone
[2018-07-16 22:51:00,583: INFO/MainProcess] celery@zhiliangdeMacBook-Pro.local ready.
PS,redis這里選做Celery的broker和backend梁只,如果沒有配置backend缚柳,如果不需要處理結(jié)果,tasks也是可以正常運行搪锣,只是結(jié)果沒有存儲的地方秋忙,當你調(diào)用ready()函數(shù)的時候,就會報錯构舟,這里應(yīng)當注意下灰追。
>>> from tasks import add
>>> a=add.delay(1,5)
>>> a
<AsyncResult: 587c76ea-a441-417d-922f-1da860c07762>
>>> a.ready()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/li/.venv/venv-myprojet/lib/python3.7/site-packages/celery/result.py", line 311, in ready
return self.state in self.backend.READY_STATES
File "/Users/li/.venv/venv-myprojet/lib/python3.7/site-packages/celery/result.py", line 471, in state
return self._get_task_meta()['status']
File "/Users/li/.venv/venv-myprojet/lib/python3.7/site-packages/celery/result.py", line 410, in _get_task_meta
return self._maybe_set_cache(self.backend.get_task_meta(self.id))
File "/Users/li/.venv/venv-myprojet/lib/python3.7/site-packages/celery/backends/base.py", line 359, in get_task_meta
meta = self._get_task_meta_for(task_id)
AttributeError: 'DisabledBackend' object has no attribute '_get_task_meta_for'
>>>
這里只是需要配置上backend接收處理結(jié)果就可以。