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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With