Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nonblocking django? [closed]

At work I'm not allowed to use perl for web services. Python is allowed however.

What I need to do is serve up the results of some very slow c++ binaries. Each exe takes up to 20 seconds to run. In perl I'd just use mojolicious's non blocking event loop ( an example.of which is given here. http://blogs.perl.org/users/joel_berger/2014/01/writing-non-blocking-applications-with-mojolicious-part-3.html )

How would one go about doing this with django and python?

like image 325
tlrrd Avatar asked Jan 26 '14 10:01

tlrrd


People also ask

Can I use Asyncio in Django?

Django uses asyncio. iscoroutinefunction to test if your view is asynchronous or not. If you implement your own method of returning a coroutine, ensure you set the _is_coroutine attribute of the view to asyncio. coroutines.

Is Django blocking or non-blocking?

Also, this method won't technically be non-blocking as Django is not a non-blocking framework in most deployments, but it will let you do something after returning a response. Tornado will let you perform non-blocking requests.

Is Django 4 asynchronous?

Latest version of the popular Python web framework also provides an asynchronous interface for all data access operations. Django 4.1, a new version of the major Python-based web framework, adds capabilities such as asynchronous handlers and an ORM interface but also makes some backward-incompatible changes.

How do I use async view in Django?

http import HttpResponse async def index(request): return HttpResponse("Hello, async Django!") Creating async views in Django is as simple as creating a synchronous view -- all you need to do is add the async keyword. The --reload flag tells Uvicorn to watch your files for changes and reload if it finds any.


2 Answers

Tornado using non blocking IO , the concepts are the same as in perl or node js event loop, multiple tasks per thread and so on.

like image 177
Slow Harry Avatar answered Nov 07 '22 16:11

Slow Harry


Probably won't be possible with Django, as the entire framework will need to be built specifically for running inside an event loop. In an event-driven framework, slow operations (I/O for example) needs to be implemented using callbacks, so that the actual I/O can be offloaded to the event loop itself, and the callback only called when the operation has finished; Django is not implemented like this.

Take a look at Twisted — it is an event-driven networking engine for Python that also has some web application frameworks built on top of it.

like image 39
lanzz Avatar answered Nov 07 '22 16:11

lanzz