Comments (7)
Hi @Adamantish
I'm not really expert on gunicorn and do not know how it patches django, but in general if you use regular sync operations from django-ninja nothing is really different from regular django views, so I guess my answer is yes - nothing to worry about.
My usual approach is to write all in sync mode, and once some operation is getting high concurrency and does some IO - then it a good candidate to switch it to async mode.
from django-ninja.
Thanks @vitalik, I had I look around to try to understand why this is the case and I think this question on FastApi was essentially the same as mine but more clearly formed and aso answered itself.
When you don't define a view as async in FastAPI it will be sent to run in a pool of OS threads, much like gunicorn in threaded mode. I took a little look and once I got to here in ninja it seems like confirmation that this doesn't do something similar. i.e. is it true that views not declared with async
will block the process while waiting on I/O? Because that would be very different to normal django use.
from django-ninja.
is it true that views not declared with
async
will block the process while waiting on I/O?
Not really
if you define our ninja "view" as async - the AsyncOperation will be used.
Both are just wrappers for regular django views which internally follow this logic
- in async mode all async views are using event loop
- sync views will be executed in threads
Generally django-ninja is just a wrapper around django views, it does not bring any threading/event-loop features on top
read more about django async support here:
https://docs.djangoproject.com/en/3.1/topics/async/#async-views
from django-ninja.
Ah so sync views will be done in threads? The reason I ask is that with standard Django the app is single threaded and it's the web server such as gunicorn that's responsible for multithreading / gevent loop. Uvicorn, on the other hand, I couldn't find a threaded feature for which is why FastAPI uses asyncio's ThreadPoolExecutor
via run_in_executor
.
Is there somewhere in ninja that does similar?
from django-ninja.
I kind of repeat my self - but django-ninja is not involved in any sync/async/thread behaviour - it just a validation/openapi wrapper
All the execution logic is described here:
https://docs.djangoproject.com/en/3.1/topics/async/#async-views
I cannot guaranty this 100% - but my guess is that if you run django 3.1+ project under asgi (uvicorn or dafne) - sync views are executed under asgiref.sync_to_async
(see https://github.com/django/asgiref/blob/master/asgiref/sync.py )
and it look like the execution is actually happening in threadpool executor
from django-ninja.
Ah thank you very much. This makes a lot more sense and seems a sensible separation. Forgive my ignorance, I had the weird assumption that Django didn't have anything built-in to use an ASGI server.
from django-ninja.
Hey @Adamantish , how did this work out for you?
from django-ninja.
Related Issues (20)
- Trivial question about external requests HOT 3
- Cache the OpenAPISchema for faster responses HOT 7
- Avoid double validation when returning ninja schema instances from the view HOT 1
- [BUG] Mocked request object not using HttpRequest as spec
- too many values to unpack (expected 2) HOT 8
- [BUG] Running tests using Schema model generates warning
- [BUG] Awkward router behaviour when path is like '/{some_var}' HOT 1
- After adding the blacklist successfully, the token is still authenticated successfully HOT 1
- [BUG] Sync-only Authentication Callbacks not Working on Async Operations HOT 1
- [BUG] Mock Request Generated by Test Client Doesnโt Support auser()
- RealWorld Demo for Django Ninja HOT 2
- Is there a way to use swagger and redoc together? HOT 1
- Websocket please!
- Inconsistent Documentation Regarding Resolver HOT 1
- Easy way to change behavior of `BodyModel`? HOT 2
- [BUG] Converter `uuid` is already registered - deprecated in django 6.0 HOT 1
- Make 'exclude_none' and other options easier to enable per router
- [BUG] Field serialized when using repr=False HOT 1
- How to create an alias for a field created via annotation HOT 2
- [BUG] model_dump defaults to `mode=python`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-ninja.