关于消息队列的一些想法和积累

关于消息队列的一些想法和积累

名字 官网 备注
celery https://github.com/celery/celery Celery 5.x >= CPython 3.5
huey https://github.com/coleifer/huey 轻量化,支持2.*
dramatiq https://github.com/Bogdanp/dramatiq >=3.5 https://dramatiq.io

设计

任务长度应该尽量小

长耗时任务还带来更新耗时更长的问题。当需要更新程序的时候,由于还有任务在执行,只能使用kill -9 **** 来强制结束进程(带来不可恢复的任务终止,在某些情况下是不可采取的操作)。

不可避免的长耗时任务处理方法

长耗时任务虽然不能说不好但应该尽量避免。
长耗时任务应该被分割开到独立的队列,并启动只处理指定队列的worker。

绑定Queue的task

1
2
3
 @app.task(queue='add')
def add(x, y):
return 2*x + 2*y

启动只处理指定队列的worker

1
celery -A tasks worker -Q add

并发模式

ISSUE

ROOT模式上不能启动

Running a worker with superuser privileges when the
worker accepts messages serialized with pickle is a very bad idea!

If you really want to continue then you have to set the C_FORCE_ROOT
environment variable (but please think about this before you do).