Giter Club home page Giter Club logo

Comments (7)

vitalik avatar vitalik commented on September 25, 2024

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.

Adamantish avatar Adamantish commented on September 25, 2024

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.

vitalik avatar vitalik commented on September 25, 2024

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.

Adamantish avatar Adamantish commented on September 25, 2024

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.

vitalik avatar vitalik commented on September 25, 2024

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.

Adamantish avatar Adamantish commented on September 25, 2024

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.

nythrox avatar nythrox commented on September 25, 2024

Hey @Adamantish , how did this work out for you?

from django-ninja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.