windows驅動sleep_Celery非同步作windows定時任務
阿新 • • 發佈:2021-02-12
技術標籤:windows驅動sleep
安裝python依賴包
pip install celery == 3.1.18pip install redis == 2.10.6
因為window不支援celery4.0以上所以指定版本安裝,windows上安裝redis可以參考我的另一篇文章:https://www.jianshu.com/p/bfb5e45586c5
手動驅動任務示例,相關的檔案主要涉及:
celeryconfig.py # 任務配置檔案__init__.py #例項化celery物件task1.py #具體的任務實體task2.py #具體的任務實體client.py #任務的生產提交客戶端
圖示:
image
init.py 檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 14:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""from celery import Celeryapp = Celery('demo') # 建立 Celery 例項app.config_from_object('celery_app.celeryconfig') # 通過 Celery 例項載入配置模組
celeryconfig.py檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 14:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""BROKER_URL = "redis://localhost:6379/0" # 指定 BrokerCELERY_RESULT_BACKEND = "redis://localhost:6379/1" # 指定 BackendCELERY_TIMEZONE = 'Asia/Shanghai' # 指定時區,預設是 UTC# CELERY_TIMEZONE='UTC'CELERY_IMPORTS = ( # 指定匯入的任務模組 'celery_app.task1', 'celery_app.task2')
task1.py檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 14:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""import timefrom celery_app import [email protected] add(x, y): time.sleep(2) return x + y
task2.py檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 15:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""import timefrom celery_app import [email protected] multiply(x, y): time.sleep(2) return x * y
client.py檔案內容
#!/usr/bin/evn python# coding=utf-8# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + """Author = [email protected]_Time: 2018/1/11 15:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""import datetimefrom datetime import timedeltafrom celery_app import task1from celery_app import task2task1.add.apply_async(args=[2, 8]) # 也可用 task1.add.delay(2, 8)task2.multiply.apply_async(args=[3, 7]) # 也可用 task2.multiply.delay(3, 7)print('hello world')# countdown:指定多少秒後執行任務task1.add.apply_async(args=(2, 23), countdown=5) # 5 秒後執行任務task1.add.apply_async(args=[6, 7], expires=10) # 10 秒後過期# 當前 UTC 時間再加 10 秒後執行任務# task1.add.multiply.apply_async(args=[8, 7], eta=datetime.utcnow() + timedelta(seconds=10))# task1.add.apply_async(args=[8, 7], eta=datetime.utcnow() + timedelta(seconds=10))# expires:任務過期時間,引數型別可以是 int,也可以是 datetime# task1.add.multiply.apply_async(args=[6, 7], expires=10) # 10 秒後過期# task1.add.apply_async(args=[6, 7], expires=10) # 10 秒後過期
執行的方式:
1:進入到目錄:
cd celery_demo2
2: 啟動 Celery Worker 程序,在專案的根目錄下執行下面命令:
celery_demo2 $ celery -A celery_app worker --loglevel=info
3: 執行客戶端:
執行client.py,檢視對應的調式的資訊
bast定時任務驅動任務示例,相關的檔案主要涉及:
celeryconfig.py # 任務配置檔案task1.py #具體的任務實體task2.py #具體的任務實體
image
celeryconfig.py檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 15:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""from datetime import timedeltafrom celery.schedules import crontab# Broker and BackendBROKER_URL = "redis://localhost:6379/2" # 指定 Broker# CELERY_RESULT_BACKEND = "redis://localhost:6379/3" # 指定 Backend# TimezoneCELERY_TIMEZONE = 'Asia/Shanghai' # 指定時區,不指定預設為 'UTC'# CELERY_TIMEZONE='UTC'# importCELERY_IMPORTS = ( 'celery_app.task1', 'celery_app.task2')# schedulesCELERYBEAT_SCHEDULE = { 'add-every-30-seconds': { 'task': 'celery_app.task1.add', 'schedule': timedelta(seconds=3), # 每 30 秒執行一次 'args': (5, 8) # 任務函式引數 }, 'multiply-at-some-time': { 'task': 'celery_app.task2.multiply', 'schedule': crontab(hour=9, minute=50), # 每天早上 9 點 50 分執行一次 'args': (3, 7) # 任務函式引數 }}
task1.py檔案內容
#!/usr/bin/evn python# coding=utf-8"""Author = [email protected]_Time: 2018/1/11 15:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""import timefrom celery_app import [email protected] add(x, y): time.sleep(2) return x + y
task2.py檔案內容
#!/usr/bin/evn python# coding=utf-8# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +# ┏┓ ┏┓+ +# ┏┛┻━━━┛┻┓ + +# ┃ ┃ # ┃ ━ ┃ ++ + + +# ████━████ ┃+# ┃ ┃ +# ┃ ┻ ┃# ┃ ┃ + +# ┗━┓ ┏━┛# ┃ ┃ # ┃ ┃ + + + +# ┃ ┃ Codes are far away from bugs with the animal protecting # ┃ ┃ + 神獸保佑,程式碼無bug # ┃ ┃# ┃ ┃ + # ┃ ┗━━━┓ + +# ┃ ┣┓# ┃ ┏┛# ┗┓┓┏━┳┓┏┛ + + + +# ┃┫┫ ┃┫┫# ┗┻┛ ┗┻┛+ + + +# + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +""""""Author = [email protected]_Time: 2018/1/11 15:[email protected]: [email protected]: [email protected]@File: [email protected]檔案功能描述:"""import timefrom celery_app import [email protected] multiply(x, y): time.sleep(2) return x * y
執行的方式:
1:進入到目錄
cd celery_periodic_tasks
2: 啟動 Celery Beat 程序,定時將任務傳送到 Broker,在專案根目錄下執行下面命令:
celery_periodic_tasks $ celery beat -A celery_app
3: 啟動 Celery Worker 程序,在專案的根目錄下執行下面命令:
celery_periodic_tasks $ celery -A celery_app worker --loglevel=info