Celery
Task Management
Web Development
Python
Background Tasks

Interact with celery ongoing task

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Celery is a powerful, production-ready asynchronous job queue, which allows you to run time-consuming Python functions in the background. A common use case is to perform background processing of computations or managing time-intensive tasks outside the main application flow. Interacting with ongoing tasks is a vital capability of Celery, providing flexibility and insights into task progression, modifications, or even termination.

Understanding Task States in Celery

Celery manages task state transitions, which is crucial for understanding how to interact with ongoing tasks. These states provide feedback on task progress and outcome. The primary states are:

  • PENDING - The initial state of a task upon creation.
  • STARTED - The task has begun execution.
  • SUCCESS - The task executed successfully, and the result is available.
  • FAILURE - The task raised an exception.
  • RETRY - The task is scheduled to be retried.
  • REVOKED - The task has been terminated.

Fetching Task Status and Results

To interact with an ongoing task, you typically want to check its status or fetch its result. Using Celery's AsyncResult class, you can monitor these aspects:

python
1from celery.result import AsyncResult
2from my_celery_app import app
3
4task_id = "some-task-id"
5task_result = AsyncResult(task_id, app=app)
6
7print("Status:", task_result.status)
8print("Result:", task_result.get(timeout=1))

The AsyncResult.get(timeout=None) method can be configured with or without timeout. Using timeout, it either returns the result if the task has finished or raises a celery.exceptions.TimeoutError if no result is available within the specified period.

Stopping or Revoking Tasks

Sometimes a situation demands the termination of an ongoing task. You can achieve this using the revoke() method:

python
app.control.revoke(task_id, terminate=True)

Setting terminate=True will immediately terminate the task. Without it, the task will only be told to halt after its current execution cycle.

Example of Task Interaction

Consider a simple task that adds two numbers but sleeps for a bit between adding and returning:

python
1@app.task(bind=True)
2def add(self, x, y):
3    import time
4    time.sleep(5)
5    return x + y
6
7# Start task
8result = add.delay(4, 5)
9
10# Revoke task before completion
11revoke(result.id, terminate=True)

Enhancements with Callbacks and Updates

Celery supports enhancing task interaction by using callbacks, chain (for linking tasks), and task update mechanisms during execution:

python
1@app.task(bind=True)
2def add(self, x, y):
3    self.update_state(state="PROGRESS", meta={'current': 60, 'total': 100})
4    return x + y

Callbacks can be added during task calls which will execute upon task completion.

python
result = add.apply_async((2, 3), link=some_other_task.si())

Summary Table

FeatureMethod / AttributeDescription
Check Task StatusAsyncResult.statusFetches current status of a task.
Get Task ResultAsyncResult.get(timeout=None)Gets the task result, option for timeout.
Revoke Taskapp.control.revoke(task_id)Stops a task. Terminates if terminate=True.
Update Taskself.update_state()Inside task, updates task state information.
Link Callbacksapply_async(link=callback)Executes a callback upon task completion.

Conclusion

Leveraging Celery for task management in your applications not only enhances performance but also introduces robustness by appropriately interacting with ongoing tasks. From revoking, checking status, to applying callbacks, the beneficial features of Celery empower developers with comprehensive control over background task execution. This adeptness in managing tasks is crucial for complex applications, particularly where task dependency and execution order play a crucial role.

Using these methods and strategies, Celery can effectively support heavy backend tasks, thus maintaining the responsiveness and performance of your main application flow.


Course illustration
Course illustration

All Rights Reserved.