question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ATOMIC_REQUESTS and async views

See original GitHub issue

First of all, thank you very much for your work on this project. I sincerely hope that you and your family are doing well and I sincerely pray for the resolution of the conflict in Ukraine.

Describe the bug I’m getting a RuntimeError on my django ninja async views when ATOMIC_REQUESTS is true

Versions

  • Python version: 3.10
  • Django version: 4.0.3
  • Django-Ninja version: 0.17.0
# settings.py
DATABASES["default"]["ATOMIC_REQUESTS"] = True

# api.py


@transaction.non_atomic_requests
@router.post("/flight-hotel-search", response=FlightHotelSearchResult)
async def flight_hotel_search(request:HttpRequest, criteria: FlightHotelSearchCriteria):
  # there is zero database call done by me in this view
  ...



# Error
# RuntimeError: You cannot use ATOMIC_REQUESTS with async views.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
benclearycommented, Mar 21, 2022

Ah that makes sense, yeah its one of the big gotchas of async and Django, until the ORM is finished its rough to get it all playing together.

0reactions
Tobi-Decommented, Mar 21, 2022

I could just be missing a few things, are these async call’s external services? or the database? Also what is your need for the atomic requests?

yes, they were suppose to be async http call to external services. The atomic requests is on the entire project. I have this setting on by default on all of my projects. I plan to use async code in only one view anyway, it doesn’t make sense to me to disable atomic requests for the sole purpose of making one specific view work

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous support — Django 4.1.4 documentation
Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views...
Read more >
Django 3.1 can't manage to run views asynchronously
Under a WSGI server, async views will run in their own, one-off event loop. is also applied to ASGI mode. Here's the code...
Read more >
Is it possible to use transaction.atomic with async functions?
I'm trying to use select_for_update in an async application. It requires a transaction to work, but I can't find a way to use ......
Read more >
Fixing your Django async job - database integration - /var/
Another solution would be to set ATOMIC_REQUESTS to True and disable atomic requests for the specific view that starts the asynchronous job ...
Read more >
Async Views in Django - TestDriven.io
This tutorial looks at how to get started with Django's asynchronous views.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found