-
-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to register tortoise on a celery app? #805
Comments
no, which don't support asyncio |
@lgarcia11 I use these tricks for my Celery periodic tasks to use Tortoise ORM: async def wrap_db_ctx(func: Callable, *args, **kwargs) -> None:
try:
await connect_db()
await func(*args, **kwargs)
finally:
await disconnect_db()
def async_to_sync(func: Callable, *args, **kwargs) -> None:
asyncio.run(wrap_db_ctx(func, *args, **kwargs))
@celery_app.task
def my_task() -> None:
async_to_sync(my_async_func) Just register Tortoise ORM in |
Which can be a solution |
I was trying to do the same thing and this ended up working for me: import asyncio
from celery import current_app as proj_celery_app
from celery.schedules import crontab
from tortoise import Tortoise
from proj import settings
from proj.tasks import task
async def create_celery(celery_app=proj_celery_app):
celery_app.config_from_object(settings.celery.settings)
celery_app.conf.update(task_acks_late=True)
celery_app.conf.update(task_default_priority=5)
celery_app.conf.update(task_queue_max_priority=10)
celery_app.conf.beat_schedule = {
# Executes daily at midnight.
"backend_cleanup": {
"task": "proj:cache_items",
"schedule": crontab(minute=0, hour=0),
"args": (),
"options": {
"priority": 10,
"queue": "proj"
}
}
}
await Tortoise.init(config=settings.db.TORTOISE_CONFIG)
return celery_app
celery_app = asyncio.run(create_celery(celery_app=proj_celery_app)) And then starting the services: In short, adding If someone else comes across this in a search, hopefully this will help. I provided additional details in this StackOverflow answer. |
how to write these functions |
async def connect_db() -> None: async def disconnect_db() -> None: |
@MLBZ521 Thanks for that answer, it saved my day. I improved it by using def create_celery():
celery_app = Celery(
__name__,
broker=settings.CELERY_BROKER_URL,
backend=settings.CELERY_RESULT_BACKEND,
)
run_async(Tortoise.init(config=TORTOISE_ORM_CONFIG))
run_async(Tortoise.generate_schemas())
return celery_app
celery = create_celery() This way, it doesn't need to be wrapped by |
Where did you get |
@Madi-S |
Can Tortoise ORM be used from a celery task, if so what is the best way of registering it?
The text was updated successfully, but these errors were encountered: