You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Django Ninja's AsyncOperation blocks the response when using asyncio.create_task() or asyncio.ensure_future() in an async view. Even though these tasks are meant to run in the background, the operation waits for them to complete before sending the response.
Example code:
@router.post("/generate")asyncdefgenerate_daily_summary(request):
logger.info("Starting tasks")
asyncio.create_task(background_task_1())
asyncio.create_task(background_task_2())
logger.info("Tasks created") # This logs immediatelyreturn200, None# Response is delayed until tasks complete
The issue appears to be in AsyncOperation.run() where it awaits the entire view function, including any background tasks created within it.
Versions
Python version: 3.12
Django version: 5.1
Django-Ninja version: 1.3.0
Pydantic version: 2.5.2
Proposed Solution
Add support for non-blocking background tasks, or consider adding a decorator or parameter to mark certain tasks as background operations that shouldn't block the response:
I’m kinda disappointed by this behavior and would appreciate some clarity. Could you help me understand if I’ve misunderstood something? It seems that this behavior simply defeat the purpose of using an async view.
Describe the bug
Django Ninja's AsyncOperation blocks the response when using asyncio.create_task() or asyncio.ensure_future() in an async view. Even though these tasks are meant to run in the background, the operation waits for them to complete before sending the response.
Example code:
The issue appears to be in AsyncOperation.run() where it awaits the entire view function, including any background tasks created within it.
Versions
Proposed Solution
Add support for non-blocking background tasks, or consider adding a decorator or parameter to mark certain tasks as background operations that shouldn't block the response:
The text was updated successfully, but these errors were encountered: