Celery
Schedules periodic tasks with beat and executes them on worker nodes.
- Downloads / mo
- 53.6M
- Stars
- 29.2K
- Last release
- 5.7.0
- License
- BSD-3-Clause
Strengths
- Separates scheduling from task execution. Let beat enqueue periodic tasks for worker nodes to execute.
- Supports cron-style periodic schedules. Use crontab schedules for tasks that run at chosen calendar times.
Limitations
- Each schedule needs one beat. Run one scheduler for each schedule to avoid duplicate task submissions.
- Periodic tasks can overlap. Use a lock when a periodic task can outlast its interval.
Quickstart
# tasks.py from celery import Celery app = Celery("tasks", broker="redis://localhost:6379/0") @app.task def add(x, y): return x + y # producer.py import tasks result = tasks.add.delay(1, 2)
Celery examples
def finish_import_project(self, request, project): """ Perform last steps to import a project into Read the Docs. - Add the user from request as maintainer - Run extra tasks that are needed before building the project - Trigger the initial build Direct upload projects skip the last two steps: Read the Docs never builds them, and the webhook and SSH key are not needed. It requires the Project was already saved into the DB. :param request: Django Request object :param project: Project instance just imported (already saved) """ project.users.add(request.user) log.info( "Project imported.", project_slug=project.slug, user_username=request.user.username, ) if project.is_direct_upload: return None update_docs, build = prepare_build(project) if (update_docs, build) == (None, None): return None tasks = self._get_post_import_tasks(project, request.user) # Build-isolated builds are dispatched by ``admit_queued_builds``, # running ``update_docs`` too would build it twice on two builders. if not project.has_feature(Feature.USE_BUILD_ISOLATED): tasks.append(update_docs) if tasks: chain(*tasks).apply_async()