背景:項目在公司的一臺虛擬機上運行(32核+32G)。其餘人的項目也在這臺物理機上運行。。個人訓練代碼是單進程的,跑完一次須要大約10h(數據量大逮着一個核使勁跑。。);訓練是一個Celery定時任務;我開始訓練時就有人提出他們的項目慢的卡着了。。html
改進:用多進程改進了訓練過程當中阻塞的地方。這時就出問題了,在Celery進程中運行建立子進程時報錯:AssertionError: daemonic processes are not allowed to have children(「不容許在守護進程中建立子進程」)
python
解決辦法:web
1,在終端設置環境變量啓用優化模式,export PYTHONOPTIMIZE=1,再執行celery -A app.celery.base worker -l info -n socwebai就好了ubuntu
2,若是用的multiprocessing,重寫一個Mypool:https://stackoverflow.com/questions/6974695/python-process-pool-non-daemonic(沒試)
bash
用方法1能夠在本地測試運行了。服務器
修改服務器上supervisor app
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’this
試了下面幾條命令,仍是提示不能建立子進程
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終端被佔用,沒法繼續向下執行,該部分的做用是:把當前終端放到後臺,繼續向下執行。