Celery學習--- Celery在項目中的使用

能夠把celery配置成一個應用,注意鏈接文件命名必須爲celery.py html

目錄格式以下 node

image

項目前提: 安裝並啓動Redis python

image

CeleryPro/celery.py   【命名必須爲celery.py】redis

# 將相對路徑轉換爲絕對路徑
from __future__ import absolute_import, unicode_literals
from celery import Celery
# 定義了一個Celery的App
app = Celery('tasks',
             # redis://:password@hostname:port/db_number  有密碼認證的鏈接
             broker='redis://192.168.2.105',
             # broker='redis://:密碼@192.168.2.105:6379/0',
             backend='redis://192.168.2.105',  # 用於Celery的返回結果的接收
             include=['CeleryPro.myTasks']       # 用於聲明Celery要執行的tasks任務的位置
             )
# Optional configuration, see the application user guide.
app.conf.update(
    result_expires=3600,   # Celery結果存在中間件Redis的超時時間[僅針對當前的Celery的App]
)
if __name__ == '__main__':
    app.start()

CeleryPro/myTasks.pysql

# 將相對路徑轉換爲絕對路徑
from __future__ import absolute_import, unicode_literals
from .celery import app   # 從個人Celery中導入App

@app.task
def add(x, y):
    return x + y
@app.task
def mul(x, y):
    return x * y
@app.task
def xsum(numbers):
    return sum(numbers)

上傳文件到Linux服務器: 服務器

找到項目的文件夾,ALT+ P上傳 app

也能夠打包爲zip文件後上傳[unzip *.zip解壓] ide

image

項目啓動,推薦後臺啓動:ui

【前臺啓動不推薦】celery -A CeleryPro worker -loglevel=info  # 注意是項目名
【前臺啓動簡寫】celery -A CeleryPro worker -l info 
【推薦啓動,後臺啓動】 celery multi start w1 -A  CeleryPro -l info

image

image

運行結果:pwa

omc@omc-virtual-machine:~$ python3
>>> from CeleryPro import mytasks
>>> myTasks.mul.delay(1,2)
<AsyncResult: f914b94d-0e92-40db-b174-f5d3f317a977>
>>> myTasks.add.delay(1,2)
<AsyncResult: 56b4369e-001e-4b27-831a-4e77aeb9da30>

image

關於AttributeError: module 'CeleryPro' has no attribute 'celery'報錯的解決:【耗時1.0H+】

image

問題解決:更改項目中myCelery.py文件爲celery.py

問題總結:在項目中,celery的鏈接文件命名必須爲celery,不然系統查找不到

後臺啓動/中止多個Celery的worker

啓動命令:

前臺啓動命令: celery -A 項目名worker -loglevel=info 

後臺啓動命令: celery multi start w1 -A 項目名 -l info 

後臺重啓命令: celery multi start w1 -A 項目名 -l info 

後臺中止命令: celery multi stop w1 -A 項目名 -l info 

先後臺的區別: 後臺是mult啓動

注意:1. w1是worker的名稱,在啓動的時候已經設定好的

          這個跟單獨啓動時worker同樣,都是名稱而已

         2. 即使是全部的worker都已經done了,用戶任然啓動了任務,全部的任務都會保留,直到有worker來執行並返回結果。

         3. 若是前臺的用戶請求[也就是沒有帶mult啓動的celery]斷開,則前臺的worker任務也會消失。若是是帶mult啓動的celery,也就是後臺啓動,斷開鏈接後後臺的任務任然在。

後臺啓動多個Celery的worker

啓動多個Celery的worker
omc@omc-virtual-machine:~/Celery$  celery multi start w1 -A CeleryPro -l info
celery multi v4.1.0 (latentcall)
> Starting nodes...
> w1@omc-virtual-machine: OK
omc@omc-virtual-machine:~/Celery$  celery multi start w2 -A CeleryPro -l info 
celery multi v4.1.0 (latentcall)
> Starting nodes...
> w2@omc-virtual-machine: OK
omc@omc-virtual-machine:~/Celery$  celery multi start w3 -A CeleryPro -l info 
celery multi v4.1.0 (latentcall)
> Starting nodes...
>w3@omc-virtual-machine:OK

image

重啓Celery的多個worker

omc@omc-virtual-machine:~/Celery$ celery multi restart w3 -A CeleryPro -l info
celery multi v4.1.0 (latentcall)
> Stopping nodes...
> w3@omc-virtual-machine: TERM -> 2152
> Waiting for 1 node -> 2152.....
> w3@omc-virtual-machine: OK
> Restarting node w3@omc-virtual-machine: OK
> Waiting for 1 node -> None...or stop it:

image

中止Celery的多個worker

omc@omc-virtual-machine:~/Celery$ celery multi stop w3 -A CeleryPro -l info       celery multi v4.1.0 (latentcall)
> Stopping nodes...
> w3@omc-virtual-machine: TERM -> 2168$ celery multi stopwait w1 -A proj -l info

image

查看當前還有多少個Celery的worker

omc@omc-virtual-machine:~$ ps -ef|grep celery

image

注意:我的感受kill掉進程也是會幹掉celery的進程的

相關文章
相關標籤/搜索