Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created October 21, 2022 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absent1706/b1015a7d4230ed757e5095c090a54769 to your computer and use it in GitHub Desktop.
Save absent1706/b1015a7d4230ed757e5095c090a54769 to your computer and use it in GitHub Desktop.
run and retry Celery tasks synchronously
"""
useful for local development and unit tests when tasks are run syncronously
"""
from your_app.settings import TESTING
from celery import Task
from celery.canvas import Signature
def run_task(task_signature: Signature):
if TESTING:
task_signature()
else:
task_signature.delay()
def retry_task(task: Task, task_kwargs, retry_kwargs):
if TESTING:
task.request.retries += 1
task(**task_kwargs)
else:
task.retry(**retry_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment