分開使用 Celery beat and worker


背景

之前項目中采用djcelery, 將定時的任務存儲在數據庫中並且在啟動 worker 的同時啟動 beat, 命令如下:

#python manage.py celery worker -B -l info -f ./log/celery_beat_worker.log

 

這樣做只能啟動一個worker, 在自己調試的時候很方便,但是上到生產環境時,由於任務多,有可能導致任務處理不及時。

 

官方說明如下:

You can also start embed beat inside the worker by enabling workers -B option, this is convenient if you will never run more than one worker node, but it’s not commonly used and for that reason is not recommended for production use:

 

解決方法

將 beat 和 worker 分開處理:

 

#nohup python manage.py celery beat --detach --pidfile= -S djcelery.schedulers.DatabaseScheduler -l info -f ./log/celery_beat.log 

#nohup python manage.py celery worker -l info -f ./log/celery_beat_worker.log

 

參數說明:

--detach: Detach and run in the background as a daemon.
--pidfile=: 此處置為空,是因為每次運行beat會產生一個celerybeat.pid文件,當第二次運行時發現這個文件就不會再啟動beat-S: djcelery.schedulers.DatabaseScheduler 由於將beat和worker分開后,原來djcelery在django目錄下settps.py中的設定不會再被默認加載,所以指定定時任務存放在數據庫中

    -f: 指定log存放的路徑,例如:./log/celery_beat.log

 

參考

https://docs.celeryproject.org/en/3.1/django/first-steps-with-django.html

https://blog.csdn.net/qq_34195441/article/details/106527063


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM