二、Celery基本配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luhu124541/article/details/88541303
1、安装celery的redis依赖:pip install -U "celery[redis]"
2、配置redis位置:app.conf.broker_url = "redis://:password@hostname:port/db_number"
使用Unix套接字连接:redis+socket:///path/to/redis.sock
virtual_host参数添加到url:redis+socker:///path/to/redis.sock?virtual_host=db_number
http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html
3、可见性超时时间定义了等待职程在消息分派到其他职程之前确认收到任务的秒数:
app.conf.broker_transport_options = {'visibility_timeout': 3600}	#默认一个小时
4、在redis中存储状态和返回任务值:app.conf.broker_backend = redis://localhost:6379/0	#格式同2
5、广播信息默认对所有虚拟主机可见。你需要设置一个传输选项来给消息加上前缀,这样消息只会被
活动的虚拟主机收到:
app.conf.broker_transport_options = {'fanout_prefix': True} 
6、默认情况下,工作人员将收到所有与任务相关,作人员只能订阅与工作人员相关的事件:
app.conf.broker_transport_options = {'fanout_patterns': True}
7、如果任务没有在 可见性超时 内确认接收,任务会被重新委派给另一个职程并执行。
这会在预计到达时间/倒计时/重试这些执行时间超出可见性超时时间的任务上导致问题;事实上如果超
时,任务将循环重新执行。
所以你需要增大可见性超时时间,以符合你计划使用的最长预计到达时间。
注意 Celery 会在职程关闭的时候重新分派消息,所以较长的可见性超时时间只会造成在断电或强制终止
职程之后“丢失”任务重新委派的延迟。

周期任务不会受可见性超时影响,因为这是一个与预计到达时间/倒计时不同的概念。
app.conf.broker_transport_options = {'visibility_timeout': 43200}	#该值必须是int类型,单位为秒

8、Redis 在某些情况会从数据库中驱除键。如果遇到了类似这样的错误:
InconsistencyError: Probably the key ('_kombu.binding.celery') has been
removed from the Redis database.
配置redis-server的timeout参数为0

猜你喜欢

转载自blog.csdn.net/luhu124541/article/details/88541303
今日推荐