Handling multiple jQuery-Ajax requests from Template to Veiw in parallel

55 views
Skip to first unread message

Subhamoy Sengupta

unread,
May 15, 2014, 12:26:42 PM5/15/14
to django...@googlegroups.com
Hi,
  

    From my Django application's template, I need to post some data to the same Django application's view, and there is a method in view that should perform further operations with them. From my JavaScript, I am trying to send a bunch of requests at the same time, like this:

$.when(function01(),
       function02(),
       function03(),
       function04()
      ).done(function() {
         /** some debug messages and whatnot **/
      });

Each of those functions has an Ajax call and entry trace messages. In my Django views.py, I am tracing my method also. From the messages in the web console, it is apparent that those functions were all called at (almost) the same time. But from the traces in the Django view method, the Ajax requests were served sequentially, one at a time.

Here is what my Django view method looks like:

# I import the python Requests library here
@ensure_csrf_cookie
def CrossDomainProxy(request):
    try:
        cross_domain_url = <some_already_known_url>
        headers = {'content-type': 'application/json', 'Authorization': request.META['HTTP_AUTHORIZATION']}
        cross_domain_response = None
        if request.POST:
            payload = dict(request.POST.iterlists())
            cross_domain_response = requests.post(cross_domain_url, headers=headers, data=payload)
        else:
            cross_domain_response = requests.get(cross_domain_url, headers=headers)
        response = HttpResponse(cross_domain_response, mimetype="application/json", status=cross_domain_response.status_code)
    except:
        reponse = HttpResponseBadRequest('<h1>bad request!</h1>')
    return response


The idea here is to bypass the same origin policy of Ajax, and it works, but the requests are handled sequentially. I believe the python Requests module is a blocking one-task-at-a-time kind of library, which could be the reason this is happening. But if I use a library like Treq, I cannot return an HTTP response object, which is mandated by Django.

I have already tried different browsers and raising the number of TCP connections allowed per server, and that is not the culprit here.

I am running Django application using uWSGI from the project root like this:

uwsgi --async 30 --ugreen --uid www-data --http :8000 --enable-threads --wsgi-fi wsgi.py --static-map /static=./static --static-map /templates=./templates --processes 10

Needless to mention, I have tried running it as the usual development server also, and if this does not do it, neither will that.

How can I get the Django view methods to handle requests coming from the same source parallelly?

Reply all
Reply to author
Forward
0 new messages