我想使用 Celery 来实现一个任务队列来执行长时间运行的任务,比如与外部 API 交互(例如用于 SMS 发送的 Twilio)。但是,我在生产和开发中使用不同的 API 凭证。
我不知道如何静态配置 Celery(即从命令行)以传递适当的 API 凭据。相关地,如果同时存在开发队列和生产队列,我的应用程序代码(启动 Celery 任务)如何指定要与哪个 Celery 队列通信?
谢谢你尽你所能的帮助。阿维
编辑:关于如何使用 celery 的 --config 选项的工作示例的额外奖励。
我这样做的方式是使用环境变量。举个简单的例子...
# 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
这个问题有一个这样做的例子,但他们说“我怎样才能以一种不那么丑陋的方式做到这一点?” 就我而言,这是完全可以接受的,而且一点也不“丑陋”。