2017-03-03 59 views
0

芹菜文檔說配置文件應該在工作目錄或python路徑中。芹菜配置文件位置

from celery import Celery 
from properties import config_file 
import sys 


sys.path.append(config_file) 
app = Celery() 
app.config_from_object(config_file.split('.')[0]) 

這裏的config_file是/opt/celery/celery_config.py。這個想法是給用戶創建配置文件的自由。該文檔說配置文件應該在工作目錄或sys路徑中。我在sys路徑中添加了config_file,但是當worker啓動時,它會拋出導入錯誤。

是否需要將config_file與創建Celery實例的模塊放在同一目錄中?

+0

你可以給性能模塊的代碼嗎? –

回答

0

沒有必要在同一目錄中有配置。

簡單的解決方案是在啓動芹菜工時改變目錄。如果您使用主管或任何其他工具來啓動worker,則可以指定要運行worker的目錄。

在這種情況下,您可以指定目錄爲/opt/celery,芹菜命令爲celery worker -l info --config=celery_config,它將從/opt/celery/celery_config.py中選擇配置。

或者,您可以將該目錄添加到sys.path。在你的芹菜模塊中,將目錄追加到sys.path中。

import sys 

from celery import Celery 


sys.path.append('/opt/celery/') 


app = Celery() 
app.config_from_object('celery_config') 

這將從/opt/celery/celery_config.py中選取配置。

+0

如果你看到我確實調用了sys.path.append(config_file),但由於某種原因,這沒有奏效。我最終使用了一個Config類對象。我通過閱讀config_file設置了這個類的屬性。 – amrx

+0

@amrx我們不應該使用'sys.path.append(dir)而不是sys.path.append(config_file)'? – ChillarAnand