Basic two kinds of task invocation methods:
Apply_async () and delay (), the former to put the parameters in a tuple or dictionary, the latter directly using parameters
Quick reference:
T.delay (ARG, Kwarg=value)
Always a shortcut to. apply_async.
T.apply_async ((ARG,), {' Kwarg ': value})
T.apply_async (countdown=10)
Executes seconds from now.
T.apply_async (Eta=now + timedelta (seconds=10))
Executes seconds from now, specifed using ETA
T.apply_async (countdown=60, expires=120)
Executes in one minute from now, but expires after 2 minutes.
T.apply_async (Expires=now + timedelta (days=2))
Expires in 2 days, set using DateTime.
Example:
Task.delay (Arg1, arg2, kwarg1= ' x ', kwarg2= ' y ')
Task.apply_async (ARGS=[ARG1, arg2], kwargs={' kwarg1 ': ' x ', ' kwarg2 ': ' Y '})
Other Features:
# #任务延时 (Set countdown parameter) # #
>>> result = Add.apply_async ((2, 2), countdown=3)
>>> Result.get () # This takes @ least 3 seconds to return
20
# #任务过期 (set expires parameter) # #
>>> # Task expires after one minute from now.
>>> Add.apply_async ((Ten, Ten), expires=60)
When a worker receives an expired task it would mark the task as revoked
# #任务重试 (Set retry parameter) # #
Add.apply_async (2, 2), Retry=true, retry_policy={
' Max_retries ': 3,
' Interval_start ': 0,
' Interval_step ': 0.2,
' Interval_max ': 0.2,
})
。 Retry Policy
A retry policy is a mapping so controls how retries behave, and can contain the following keys:
。 Max_retries
Maximum number of retries before giving up, in this case the exception that caused the retry to fail would be raised.
A value of 0 or None means it would retry forever.
The default is to retry 3 times.
。 Interval_start
Defines the number of seconds (float or integer) to wait between retries. Default is 0, which means the first retry would be instantaneous.
。 Interval_step
On each consecutive retry this number is added to the retry delay (float or integer). Default is 0.2.
。 Interval_max
Maximum Number of seconds (float or integer) to wait between retries. Default is 0.2.
# #序列化工具 (Set serializer parameter) # #
There are several serialization tools available: Json,pickle,yaml,msgpack
JSON is good, but it's a bit weak for transmitting binary data because the volume of data is larger and the available data formats are limited based on BASE64 encoding
Pickle for applications that do not require support for components developed in other languages, the transfer of binary files is more lightweight and fast
Yaml is good for cross-language compatibility and supports more data formats. But the Python module that handles it is slow
Msgpack is a new interchange format that functions like JSON, but still too young
The configuration priority for the serialization tool is as follows
1, the serializer execution option.
2. The Task.serializer attribute
3, the Celery_task_serializer setting.
e.g >>> Add.apply_async ((Ten), serializer= ' json ')
# #压缩工具 (set compression) # #
If you send a message that is larger, consider compressing the message
e.g >>> Add.apply_async ((2, 2), compression= ' zlib ')
# #不同的消息队列 #
e.g Add.apply_async (queue= ' Priority.high ')
e.g $ celery-a proj worker-l info-q Celery,priority.high
Advanced Parameters:
。 Exchange
Name of Exchange (or a kombu.entity.Exchange) to send the message to.
。 Routing_key
Routing key used to determine.
。 Priority
A number between 0 and 9, where 0 is the highest priority.
Supported By:redis, Beanstalk
#sora #celery notes--call the task