您好,登錄后才能下訂單哦!
celery異步框架怎么在python3項目中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
安裝celery
sudo pip install celery
實例化 celery
from celery import Celery app = Celery("testapp") # 導入配置 app.config_from_object('celery_tasks.config') # 自動添加任務 app.autodiscover_tasks(["celery_tasks.test","celery_tasks.test2"])
簡單配置
# 任務隊列的地址 broker_url = "redis://127.0.0.1/14" # 任務處理結果的保存地址[如果不需要接收任務處理結果,那么,可以不設置下面] result_backend = "redis://127.0.0.1/15"
文件目錄如下
. ├── config.py ├── main.py ├── test │ └── tasks.py └── test2 ├── __init__.py └── tasks.py
一個應用一個文件夾
在需要執行該任務的地方導入該任務
from celery_tasks.test.tasks import test from celery_tasks.test2.tasks import test as test2 test_id = test.delay() test2_id = test2.delay() print(test_id) print(test2_id)
調用該異步任務會馬上放回一個id,執行結果可以在result_backend中通過id找到
/home/python/.virtualenvs/kol_site_py3/bin/python /home/python/projects/supervisor/supervisor/celery_tasks/test.py a6e13745-c05b-496d-bbbe-2b636f84009c d92d50b4-0ba1-4b05-9e96-eeb92a854929 Process finished with exit code 0
127.0.0.1:6379[15]> keys * 1) "celery-task-meta-2a9c0a4b-5b40-4121-9986-a8430fc6b235" 2) "celery-task-meta-0f16e227-393f-48ea-b41b-3419df84528e" 3) "celery-task-meta-fbf31a20-6eee-4298-8a91-214d2e5c9399" 4) "celery-task-meta-61f012c0-bde1-4344-9e1c-b5e8a7b93902" 5) "celery-task-meta-074a659f-d76f-4818-8516-f098d1b900ed" 6) "celery-task-meta-8a89c4db-f2e2-484b-94ee-e1af9911c69f" 7) "celery-task-meta-0012966d-e8fd-483b-b8ac-d160d65c8221" 8) "celery-task-meta-f97a452d-3812-4950-bfd9-02ff9e69a4b2" 9) "celery-task-meta-4bebe710-7725-43f5-b0f7-9a35b57ba3b1" 10) "celery-task-meta-4b1cca23-31c3-4c82-a99f-bbe306846191" 11) "celery-task-meta-4cdf3a68-7df4-4bdf-8f54-abe6be83df3a" 12) "celery-task-meta-d92d50b4-0ba1-4b05-9e96-eeb92a854929" 13) "celery-task-meta-17265693-ba36-4f6c-80c8-d89a52f549f7" 14) "celery-task-meta-d62bbf16-6469-40a7-bc25-61b553014d76" 15) "celery-task-meta-4cca0f47-2f2d-45e6-8341-52264e50d969" 16) "celery-task-meta-1fd1e52a-00e1-486a-a224-36bd0fbb5d4a" 17) "celery-task-meta-af3b9536-91a6-4ae3-ab9b-59755bfb4883" 18) "celery-task-meta-b5710e2a-1905-44fd-8b11-4d7057113291" 19) "celery-task-meta-bebeb902-cce1-4edb-bdac-734ed6dc16ae" 20) "celery-task-meta-2771b961-694f-4727-9b19-07928834475e" 21) "celery-task-meta-8c683476-5cec-4933-8370-73793d656e23" 22) "celery-task-meta-6c8e6763-a416-4c02-9689-a0bb38bf26a6" 23) "celery-task-meta-7a4edb71-b13b-4f0f-b882-408716bb3ba9" 24) "celery-task-meta-4e368ca3-f686-4215-aed7-f0c6463cfac9" 25) "celery-task-meta-757f196d-c377-4f38-982d-700fa4f45c6b" 26) "celery-task-meta-094ea32e-5cf8-41c5-bf63-fb629e0e1e67" 27) "celery-task-meta-2e1f2188-0806-41f1-8eb8-4a0f73ec2aca" 28) "celery-task-meta-fd7e8fea-c738-4d49-b13d-c5d782eeaa96" 29) "celery-task-meta-e476f036-7192-4687-b9b7-c6a06556b4c3" 30) "celery-task-meta-2463c15f-5903-4381-8646-1b2aa6418ca0" 31) "celery-task-meta-a6e13745-c05b-496d-bbbe-2b636f84009c" 32) "celery-task-meta-f4f2d940-3e16-4d78-a0c4-3766eb91c908" 33) "celery-task-meta-5a1eaba8-0675-4e82-aedc-fee801ff31ef" 127.0.0.1:6379[15]>
啟動celery的方法
# 最終在終端運行這個main文件 celery -A 應用包名 worker -l info # 我們當前項目,在后端項目根目錄下運行 celery -A celery_tasks.main worker -l info # 守護進程 celery multi start w1 -A celery_tasks.main -l info --logfile=./celerylog.log # 停止和重啟 分別將 start 改為 stop / restart
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。