|
1 | 1 | Asynchronous Tasks with Celery |
2 | 2 | ============================== |
3 | 3 |
|
4 | | -The project uses Celery for asynchronous tasks, with Celery beat for task scheduling. |
| 4 | +The project uses Celery for asynchronous tasks, with Celery beat for task scheduling, and Redis is used as the broker by default. |
5 | 5 |
|
6 | | -Redis is used as the broker by default. |
| 6 | +.. note:: |
7 | 7 |
|
8 | | -You can read more here on how to get started with `Celery <https://docs.celeryq.dev/en/stable/getting-started/first-steps-with-celery.html>`_ |
| 8 | + You can read more here on how to get started with `Celery <https://docs.celeryq.dev/en/stable/getting-started/first-steps-with-celery.html>`_ |
| 9 | + |
| 10 | +FastAPI comes with built-in `BackgroundTasks <https://fastapi.tiangolo.com/tutorial/background-tasks/>`_, however, they run in a single |
| 11 | +process. This won't work well for more complex tasks, especially at scale. The Celery worker and beat service can scale independently of |
| 12 | +the FastAPI application, which is how typical async applications are deployed. |
| 13 | + |
| 14 | +Configuring Celery Workers |
| 15 | +-------------------------- |
| 16 | + |
| 17 | +Celery workers can be configured to run on a specific number of workers. This is useful for scaling the application to handle more requests. |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + ... |
| 22 | +
|
| 23 | + @router.get("/ping", tags=["health"]) |
| 24 | + def pong() -> Response: |
| 25 | + hello_world_task.delay() |
| 26 | + # or a task with arguments |
| 27 | + my_task.delay(id=1) |
| 28 | + return JSONResponse({"ping": "pong!"}) |
| 29 | +
|
| 30 | +You can read more about the different application settings `here <https://docs.celeryq.dev/en/v5.2.7/userguide/application.html#application>`_. |
| 31 | + |
| 32 | + |
| 33 | +Configuring Celery Beat |
| 34 | +----------------------- |
| 35 | + |
| 36 | +Celery beat is used to schedule periodic tasks. It can be configured to run on a schedule, or to run on a fixed time interval. |
| 37 | + |
| 38 | +.. code-block:: python |
| 39 | +
|
| 40 | + # worker.py |
| 41 | +
|
| 42 | + from celery.schedules import crontab |
| 43 | +
|
| 44 | + ... |
| 45 | + app.conf.beat_schedule = { |
| 46 | + # Executes every Monday morning at 7:30 a.m. |
| 47 | + 'add-every-monday-morning': { |
| 48 | + 'task': 'src.core.tasks.add', |
| 49 | + 'schedule': crontab(hour=7, minute=30, day_of_week=1), |
| 50 | + 'args': (16, 16), |
| 51 | + }, |
| 52 | + # Executes every 3rd minute |
| 53 | + 'hello-every-third-minute': { |
| 54 | + 'task': 'src.core.tasks.hello_world_task', |
| 55 | + 'schedule': crontab(minute="*/3"), |
| 56 | + }, |
| 57 | + # Executes at sunset in Melbourne |
| 58 | + 'add-at-melbourne-sunset': { |
| 59 | + 'task': 'src.core.tasks.melbourne_add', |
| 60 | + 'schedule': solar('sunset', -37.81753, 144.96715), |
| 61 | + 'args': (16, 16), |
| 62 | + }, |
| 63 | + } |
| 64 | +
|
| 65 | +You can read more about the different schedule `types <https://docs.celeryq.dev/en/v5.2.7/userguide/periodic-tasks.html#crontab-schedules>`_. |
0 commit comments