celery指令详解

全局指令

1
2
3
4
5
6
7
8
9
全局指令:
-A APP,--app celery对象
-b BROKER,--broker broker位置
--result-backend backend位置
--loader LOADER
--config CONFIG
--workdir WORKDIR Optional directory to change to after detaching.
--no-color, -C
--quiet, -q

worker指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
worker指令:
-n HOSTNAME, --hostname HOSTNAME
设置worker的hostname

Set custom hostname (e.g., 'w1@%h'). Expands: %h
(hostname), %n (name) and %d, (domain).
-D, --detach 启动worker作为后台进程

-S STATEDB, --statedb STATEDB
Path to the state database. The extension '.db' may be
appended to the filename. Default: None
-l LOGLEVEL, --loglevel LOGLEVEL
日志记录的级别,可以选择DEBUG, INFO, WARNING,
ERROR, CRITICAL, or FATAL.


-O OPTIMIZATION
--prefetch-multiplier PREFETCH_MULTIPLIER
Set custom prefetch multiplier value for this worker
instance.

Pool 指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Pool 指令:
-c CONCURRENCY, --concurrency CONCURRENCY
处理队列的子进程数。默认值是电脑系统可用的cpu数量

-P POOL, --pool POOL 实现池的方式: prefork (default), eventlet,gevent or solo.

-E, --task-events, --events
Send task-related events that can be captured by
monitors like celery events, celerymon, and others.

--time-limit TIME_LIMIT
为任务设置一个超时时间,时间到了会直接杀死task启动另外一个task

--soft-time-limit SOFT_TIME_LIMIT
为任务设置一个超时时间,时间到了会抛出异常,并结束task

--max-tasks-per-child MAX_TASKS_PER_CHILD, --maxtasksperchild MAX_TASKS_PER_CHILD
每次可以执行的最大的任务数

--max-memory-per-child MAX_MEMORY_PER_CHILD, --maxmemperchild MAX_MEMORY_PER_CHILD
子进程最大能占据的内存数,当一个任务导致这个进程超过限制后,任务会被完成,然后进程会被替换
默认没有限制

Queue 指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Queue 指令:
--purge, --discard
在守护进程启动之前将清除所有等待的任务,所有在消息队列上的任务都会被删除

--queues QUEUES, -Q QUEUES
要为这个worker启用的队列,使用逗号进行分割,默认都启用
Example: -Q video,image

--exclude-queues EXCLUDE_QUEUES, -X EXCLUDE_QUEUES
要为这个worker禁用的队列,使用逗号进行分割
enabled. Example: -X video,image.

--include INCLUDE, -I INCLUDE
用逗号分割要导入的模块列表
Example: -I foo.tasks,bar.tasks

Features指令

1
2
3
4
5
6
7
8
9
10
Features 指令:
--without-gossip 不会订阅其他的workers events.
--without-mingle 不会在启动的时候和其他worker同步
--without-heartbeat 不发送heartbeats.
--heartbeat-interval HEARTBEAT_INTERVAL
发送worker heartbeat的时间间隔
--autoscale AUTOSCALE
通过提供max_concurrency和min_concurrency来启用自动伸缩
Example:: --autoscale=10,3 (always keep 3 processes, but grow to 10 if necessary)

Daemonization 指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Daemonization 指令:
-f LOGFILE, --logfile LOGFILE
日志文件的路径,不指定的话输出到stderr

--pidfile PIDFILE 用于存储进程的文件,如果文件已经存在并且进程还存活,就不会启动这个进程

--uid UID User id, or user name of the user to run as after
detaching.

--gid GID Group id, or group name of the main group to change to
after detaching.

--umask UMASK Effective umask(1) (in octal) of the process after
detaching. Inherits the umask(1) of the parent process
by default.

--executable EXECUTABLE
Executable to use for the detached process.

Embedded Beat 指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Embedded Beat 指令:
-B, --beat 用于运行celery的周期任务,在服务中一次只能有一个beat,在生产环境中需要将celery beat分开


-s SCHEDULE_FILENAME, --schedule-filename SCHEDULE_FILENAME, --schedule SCHEDULE_FILENAME
Path to the schedule database if running with the -B
option. Defaults to celerybeat-schedule. The extension
".db" may be appended to the filename. Apply
optimization profile. Supported: default, fair

--scheduler SCHEDULER
指定sehedule的类,默认是celery.beat.PersistentScheduler


celery –poll参数详解

1
2
3
4
5
6
7
1. prefork(默认):worker会开启多个进程来执行具体的任务实例(task instance),适合于CPU密集型应用;这会开启一个worker主进程,和一组工作进程(如果并行度设置为2,当使用ps -ef | grep celery的时候,会看到3个进程,多出来的一个就是主进程)

2. eventlet:适用于I/O密集型应用;底层使用epoll或者libevent来驱动多路复用。要注意不要在这样的worker中运行CPU密集型的任务实例。

3. gevent:类似于eventlet,基于libev或者libuv事件循环。

4. solo:接收控制指令同运行任务实例在同一个进程里执行,如果任务实例执行时间较长会阻塞控制指令请求的响应,客户端需要适度增加超时时间设置。(一般不使用)

celery中的 task_time_limit 和 task_soft_time_limit

1
2
3
4
5
6
task_time_limit:
hard模式,如果配置了10s, 10s内 task 没有执行结束,则处理这个task会被杀掉,并继续执行其他新的 task

task_soft_time_limit:
soft 模式,如果配置了10s, 10s内 task 没有执行结束,可以在 task 内捕获这个异常,并处理。也可以在全局处理,进行重试或扔死信队列等操作。
比如,在 task 中捕获异常: