close

 

Python Django2.0 使用 Supervisor 管理虛擬環境下的Celery

外部環境配置:

  • python2.7
  • supervisor
  • redis
  • nginx

虛擬環境配置:

  • Django 2.0
  • python 3.6
  • celery 3.1.16
  • uwsgi 2.0.17.1

celery 的配置可以參考此篇文章 ( 3.1.16 參照 celery 3 的配置) 

 

一 、安装Supervisor

在終端機輸入以下語法進行安裝:

pip install supervisor

二、Supervisor 配置 

輸入以下語法取得supervisor配置模板:

切換至專案虛擬環境執行: 

mkdir /path/to/env/supervisor echo_supervisord_conf > /path/to/env/supervisor/supervisord.conf

echo_supervisord_conf > supervisord.conf

可以使用vim開啟,如果有安裝 sublime也可以使用sublime開啟方便修改

接著要設置進程,要修改 Django 專案目錄位置、uwsgi、worker、beat

打開supervisord.conf後,在後方添加以下內容:   

1.設置 uwsgi 啟動 :   

[program:uwsgi-mysite]
environment=PATH="/path/to/your/env/bin",VIRTUAL_ENV="/path/to/your/mysite/",PYTHONPATH="/path/to/your/env/lib/python:/path/to/your/env/lib/python3.6/site-packages"
environment=HOME="/path/to/env",USER="root"
# 確保uwsgi命令的路徑是對的
directory=/path/to/your/mysite/
command=uwsgi --ini uwsgi.ini
numprocs=1          ;進程數
autostart=true      ;自動啟動 
autorestart=true    ;自動重啟
stopsignal=INT
stdout_logfile=/path/to/your/log_directory/site_out.log
stderr_logfile=/path/to/your/log_directory/site_err.log
stopsignal=QUIT
killasgroup=true

2.設置worker:   

[program:celery.worker] 
environment=PATH="/path/to/your/env/bin",VIRTUAL_ENV="/path/to/your/mysite/",PYTHONPATH="/path/to/your/env/lib/python:/path/to/your/env/lib/python3.6/site-packages"
environment=HOME="/path/to/env",USER="root"
;指定運行目錄 
directory=/path/to/your/mysite/
;運行目錄下執行命令
command=celery -A mysite worker --loglevel info
;啟動設置 
numprocs=1          ;進程數
autostart=true      ;當supervisor啟動時,程式將自動啟動 
autorestart=true    ;自動重啟
;停止信號,默認TERM 
stopsignal=INT
;中斷:INT (類似於Ctrl+C)(kill -INT pid),退出後將會寫文件或日誌 
;終止:TERM (kill -TERM pid) 
;掛起:HUP (kill -HUP pid),注意與Ctrl+Z/kill -stop pid不同 
;從容停止:QUIT (kill -QUIT pid) 
stopsignal=INT
stdout_logfile=/path/to/your/log_directory/celery_worker.log 
stderr_logfile=/path/to/your/log_directory/celery_worker_err.log  

 

3.設置beat:   

[program:celery.beat] environment=PATH="/path/to/your/env/bin",VIRTUAL_ENV="/path/to/your/mysite/",PYTHONPATH="/path/to/your/env/lib/python:/path/to/your/env/lib/python3.6/site-packages"
environment=HOME="/path/to/env",USER="root"
;指定運行目錄 
directory=/path/to/your/mysite/
;運行目錄下執行命令
command=celery -A mysite beat --loglevel info
;啟動設置 
numprocs=1          ;進程數
autostart=true      ;當supervisor啟動時,程式將自動啟動 
autorestart=true    ;自動重啟
;停止信號,默認TERM 
stopsignal=INT
stdout_logfile=/path/to/your/log_directory/celery_beat.log 
stderr_logfile=/path/to/your/log_directory/celery_beat_err.log  

 

三、 啟動和關閉Supervisor

1.啟動supervisor輸入如下命令,使用具體的配置文件執行:

supervisorctl -c supervisord.conf

2.進入supervisor>後 簡單介紹一些指令

  • start <name>        #Start a process
  • start all            #Start all processes
  • reload             #全部重啟
  • stop <name>        #Stop a process
  • stopall             #Stop all processes

關閉supervisord需要通過supervisor的控制器:

supervisorctl  - c supervisord.conf shutdown

重啟supervisord也是通過supervisor的控制器:

supervisorctl  - c supervisord.conf reload

shutdown後如果出錯 ,使用supervisord -c supervisord.conf再重啟即可

 

四、錯誤解決:

1.運行後找不到celery套件

(1)添加環境: 

需要在uwsgi、celery worker、celery beat添加環境:   

environment=PATH="/path/virtualenvs/mysite/bin",VIRTUAL_ENV="/path/virtualenvs/mysite",PYTHONPATH="/path/virtualenvs/mysite/lib/python:/path/virtualenvs/mysite/lib/python/site-packages"

environment=HOME="/path/to/env",USER="root"

(2)使用虛擬環境進行supervisord:

這點很重要,不然command可能會抓不到套件

 

2.運行後找不到套件中的檔案:

例如我是找不到 haystack與jieba中文分詞的 whoosh_cn_backend.WhooshEngine檔

原因是外部環境沒有清除乾淨,請將外部環境清除乾淨

並在uwsgi、celery worker、celery beat添加環境,讓程式找到虛擬環境的目錄

3. static設置錯誤

ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

STATICFILES_DIRS與 STATIC_ROOT 衝突到了

由於原本在本地運行會使用到

STATICFILES_DIRS=[
    os.path.join(BASE_DIR,'static/'),
]

只要將其移除只留下STATIC_ROOT即可

4. celery4.2 uwsgi問題

celery使用4.2版本uwsgi會無法找到套件是虛擬路徑設置的問題

在uwsgi.ini文件 將 home路徑移除

但最後supervisor卻與celery4.2無法兼容,所以同樣無用

5.supervisor運行發生問題

使用supervisor與celery4.2無法兼容,celery 3.1.8與Linux Ubuntu18.04無法兼容

最後使用celery 3.1.16才能正常使用(嘗試了兩天才擠出這辦法)

 

 

 

 

發過的問題:

Celery in virtualenv with supervisor

Django+ Celery+ uWSGI 問題

supervisor 管理 虛擬環境下celery的 問題

 

參考文章:

Asynchronous Tasks With Django and Celery

Ysh Blog 鏈接:服务器使用Supervisor后台运行Celery

arrow
arrow
    全站熱搜

    ivankao 發表在 痞客邦 留言(0) 人氣()