我这样做的方式是使用环境变量。举个简单的例子...
# By convention, my configuration files are in a "configs/XXX.ini" file, with
# XXX being the configuration name (e.g., "staging.ini")
config_filename = os.path.join('configs', os.environ['CELERY_CONFIG'] + '.ini')
configuration = read_config_file(config_filename)
# Now you can create the Celery object using your configuration...
celery = Celery('mymodule', broker=configuration['CELERY_BROKER_URL'])
@celery.task
def add_stuff(x, y):
....
你最终会像这样从命令行运行......
export CELERY_CONFIG=staging
celery -A mymodule worker
这个问题有一个这样做的例子,但他们说“我怎样才能以一种不那么丑陋的方式做到这一点?” 就我而言,这是完全可以接受的,而且一点也不“丑陋”。