記一次Celery的仇


背景:項目在公司的一台虛擬機上運行(32核+32G)。其他人的項目也在這台物理機上運行。。我的訓練代碼是單進程的,跑完一次需要大約10h(數據量大逮着一個核使勁跑。。);訓練是一個Celery定時任務;我開始訓練時就有人提出他們的項目慢的卡着了。。

改進:用多進程改進了訓練過程中阻塞的地方。這時就出問題了,在Celery進程中運行創建子進程時報錯:AssertionError: daemonic processes are not allowed to have children(“不允許在守護進程中創建子進程”)

 

解決辦法:

1,在終端設置環境變量啟用優化模式,export PYTHONOPTIMIZE=1,再執行celery -A app.celery.base worker -l info -n socwebai就行了

2,如果用的multiprocessing,重寫一個Mypool:https://stackoverflow.com/questions/6974695/python-process-pool-non-daemonic(沒試)

 

用方法1可以在本地測試運行了。

修改服務器上supervisor 

command = export PYTHONOPTIMIZE=1 && /home/ldy/workspace/socwebai/venv_socwebai/bin/celery -A app.celery.base worker -l info -n socwebai

supervisor報錯找不到export

查資料發現可以指定Celery 的 -O參數:

there are two method to solve this problem ,disable assert:
1.where celery starts set export PYTHONOPTIMIZE=1 OR start celery with this parameter -O OPTIMIZATION
2.disable python packet multiprocessing process.py line 102:
assert not _current_process._config.get(‘daemon’), \ ‘daemonic processes are not allowed to have children’

 

試了下面幾條命令,還是提示不能創建子進程

celery -A app.celery.base -Q worker -l info -n socwebai
celery -A app.celery.base worker -l info -n socwebai -Q
celery -A app.celery.base worker -l info -n socwebai -Q 1

  

不熟悉-O參數鴨!

 

 


今天,問題解決了。

放棄supervisor改用systemd開機自啟celery,ubuntu18.04 systemd開機自啟教程

將文中rc.local文件替換如下:

#!/bin/bash

echo "PowerBoot strating..." > /var/www/socwebai/PowerBoot.log

cd /var/www/socwebai/
source venv_socwebai/bin/activate

export PYTHONOPTIMIZE=1
echo "ok" >> /var/www/socwebai/PowerBoot.log
celery -A app.celery.base worker -l info -n socwebai >> /var/www/socwebai/PowerBoot.log 2>&1 &


echo "okk" >> /var/www/socwebai/PowerBoot.log
celery beat -A app.celery.tasks.train_model >> /var/www/socwebai/PowerBoot.log 2>&1 &

echo "finished!" >> /var/www/socwebai/PowerBoot.log

sudo reboot 重啟后,任務就啟動了。一個celery worker,一個celery beat.

 

重點提一下rc.local部分:“ >> /var/www/socwebai/PowerBoot.log 2>&1 &”

在沒加該部分開機自啟時,執行完celery -A app.celery.base worker -l info -n socwebai終端被占用,無法繼續向下執行,該部分的作用是:把當前終端放到后台,繼續向下執行。

 


免責聲明!

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



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