Celery


Getting Started

pip install celery==5.2.7
  • Choosing a Broker

    • RabbitMQ

    • Redis

  • App

from celery import Celery

app = Celery('tasks', broker='pyamqp://guest@localhost//')


@app.task
def add(x, y):
    return x + y
  • Run

celery -A tasks worker --loglevel=INFO

Task

  • Ignore results you don’t want

  • Avoid launching synchronous subtasks

  • Database transactions

  • Call Task

  • Serializers

    • json: strings, Unicode, floats, Boolean, dictionaries, and lists. Decimals and dates are notably missing.

    • pickle: support of all built-in Python data types (except class instances)

    • yaml: has many of the same characteristics as json, except that it natively supports more data types (including dates, recursive references, etc.). However, the Python libraries for YAML are a good bit slower than the libraries for JSON.

    • msgpack – msgpack is a binary serialization format that’s closer to JSON in features. The format compresses better, so is a faster to parse and encode compared to JSON.

  • Concurrency By default multiprocessing is used to perform concurrent execution of tasks, but you can also use Eventlet. The number of worker processes/threads can be changed using the --concurrency argument and defaults to the number of CPUs available on the machine.

Periodic Tasks

Routing Tasks

  • Create task

  • Run

Signals

  • Task Signals

    • before_task_publish

    • after_task_publish

    • task_prerun

    • task_postrun

    • task_retry

    • task_success

    • task_failure

    • task_internal_error

    • task_received

    • task_revoked

    • task_unknown

    • task_rejected

  • App Signals

    • import_modules

    • Worker Signals

    • celeryd_after_setup

    • celeryd_init

    • worker_init

    • worker_before_create_process

    • worker_ready

    • heartbeat_sent

    • worker_shutting_down

    • worker_process_init

    • worker_process_shutdown

    • worker_shutdown

  • Beat Signals

    • beat_init

    • beat_embedded_init

    • Eventlet Signals

    • eventlet_pool_started

    • eventlet_pool_preshutdown

    • eventlet_pool_postshutdown

    • eventlet_pool_apply

  • Logging Signals

    • setup_logging

    • after_setup_logger

    • after_setup_task_logger

  • Command signals

    • user_preload_options

    • Deprecated Signals

    • task_sent

Ensuring a task is only executed one at a time

Config

  • accept_content

  • result_accept_content

  • worker_concurrency

  • task_ignore_result: Xác định xem Celery có nên lưu trữ kết quả của một nhiệm vụ hay không

  • time_limit: Xác định thời gian tối đa mà một nhiệm vụ cụ thể được phép chạy trước khi bị hủy bỏ

  • task_soft_time_limit: Xác định thời gian mềm tối đa cho một nhiệm vụ

  • task_retry_delay: thử (retry) nhiệm vụ một số lần

  • task_retry_max_retries: khoảng thời gian giữa các lần tái thử

  • worker_prefetch_multiplier: Xác định số lượng tối đa các nhiệm vụ được một worker lấy từ hàng đợi (queue) mỗi lần

  • worker_max_tasks_per_child: Xác định số lượng tối đa các nhiệm vụ mà một worker thực thi trước khi bị khởi động lại

  • worker_pool: prefork, threads, solo, gevent

Last updated