Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing non-blocking requests? - Django

I have been playing with other frameworks, such as NodeJS, lately.

I love the possibility to return a response, and still being able to do further operations.

e.g.

def view(request):
  do_something()
  return HttpResponse()

  do_more_stuff() #not possible!!!

Maybe Django already offers a way to perform operations after returning a request, if that is the case that would be great.


Help would be very much appreciated! =D

like image 402
RadiantHex Avatar asked Apr 30 '10 08:04

RadiantHex


People also ask

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?

Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.

How do I handle requests in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.

Does Django have 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.


1 Answers

not out of the box as you've already returned out of the method. You could use something like Celery which would pass the do_more_stuff task onto a queue and then have it run do_more_stuff() outside of http request / response flow.

like image 78
Ross Avatar answered Nov 15 '22 17:11

Ross