6 ms·
What would be a typical use case for "Asynchronous views"?
by FreeHugs 6y ago
What would be a typical use case for "Asynchronous views"?
- nurettin 6y agoit would be useful if your code has to execute async code and await it's result from within a view. For example: create 10 tasks and return await asyncio.gather(*tasks) as json.
- FreeHugs 6y agoI don't know much about Python's async tools, but why couldn't that be done in a non-async view? In pseudo code I would think it would look like this: def my_view(request): name = async_get_name() // returns a promise city = async_get_city() // returns a promise waitUntilResolved(name,city) return HttpResponse('Hello '+name+' from '+city)
- nurettin 6y agoIt could be done with anything, but async view lets other code execute while it is waiting for IO, what does waitUntilResolved do?
- FreeHugs 6y agowaitUntilResolved() would wait until the promises are resolved and of course let other code execute meanwhile.
- nurettin 6y agoThat's an awesome function. Is it multiprocess?
- FreeHugs 6y agoIt was pseudocode. If a function like this exists, it would just sleep until the given promises are resolved. I don't see how that is realted to the term "multiprocess".
- nurettin 6y agoWell I don't really see how it can exist, do non-blocking io and wait for promises while performing a sleep. So the only way left is multiprocess.
- macNchz 6y agoIt sounds like your waitUntilResolved() function is effectively asyncio.gather(), which due to the nature of Python’s async implementation is not something that could be used in a Django view prior to this release.
- woile 6y agoasync is cool for I/O bound operations. You don't have to wait the request/response to finish in order to start processing another request. Talking with a db, or doing http requests are IO operations, so instead of blocking the process, django can now start processing another one. When the IO operation is done, it continues where it stopped.
- FreeHugs 6y agoWhy would Django not be able to process other requests while one process is waiting for IO? I don't know much about Python, but in a typical PHP/Apache setup, Apache simply starts as many processes as needed to answer all requests.
- Ralfp 6y agoApache or FCGI? In python world you have few Django processes spawned by WSGI/ASGI app container, and those processes spawn python threads to handle requests. Because python uses GIL, no more than single thread is executed at single time per process. At specific times GIL stops running bytecode for one thread and moves to next one (eg when you do IO). This means other threads keep running, but in theory you can run out of threads. In async you use tasks instead of threads for http requests, so single process can handle multiple requests at same time, only spawning threads at limited number of situations.
- Uberphallus 6y agoYes, you can add more processes (or threads), but more processes (or threads) blocked by IO equals more context switches which equals drop in performance. Not a big deal if your app is distributed and you scale horizontally, but most apps don't need scaling that way and async fits the bill without adding unneeded resources. If your workload is IO bound (and most of web development is) async improves performance in any case.
- deleted 6y ago[deleted]
- pirsquare 6y agoMessaging and live chat