86👍
✅
Just a small addition to other answers. As already stated, bound tasks have access to the task instance. One use case when this is needed are retries:
@celery.task(bind=True, max_retries=5)
def retrying(self):
try:
return 1/0
except Exception:
self.retry(countdown=5)
Another use case is when you want to define custom states for your tasks and be able to set it during task execution:
@celery.task(bind=True)
def show_progress(self, n):
for i in range(n):
self.update_state(state='PROGRESS', meta={'current': i, 'total': n})
38👍
Bound tasks
A task being bound means the first argument to the task will always be the task instance (self), just like Python bound methods:
logger = get_task_logger(__name__)
@task(bind=True)
def add(self, x, y):
logger.info(self.request.id)
- [Django]-'pip' is not recognized as an internal or external command
- [Django]-Django fix Admin plural
- [Django]-Django check for any exists for a query
16👍
The bind argument means that the function will be a “bound method” so that you can access attributes and methods on the task type instance.
See the docs
- [Django]-How to disable admin-style browsable interface of django-rest-framework?
- [Django]-With DEBUG=False, how can I log django exceptions to a log file
- [Django]-Get list item dynamically in django templates
Source:stackexchange.com