Simultaneous Web Requests appending responses in function based views

29 views
Skip to first unread message

cr katz

unread,
May 29, 2016, 7:24:17 PM5/29/16
to Django users
Hello,
        I am new to django. I developed a web application using form based views. When I submit a single web request from the dev env, the response is returned correctly. When submit 2 simultaneously from 2 browsers, I see appended responses for each request. 

How can I process requests independently?

Thanks
crkatz

James Schneider

unread,
May 30, 2016, 2:10:17 AM5/30/16
to django...@googlegroups.com

That would certainly be odd behavior, although I'm not exactly sure what you mean. Can you post the output that you're seeing and the view in question?

-James

cr katz

unread,
May 30, 2016, 6:18:42 PM5/30/16
to Django users
Hello James,
        Here below is my code:

qList in the code below, actually take a list of functions and uses the parameters received thru the request, processes and appends the response to 'p'. In this code, I eliminated those functions and hardcoded with a list of strings.

def search(request):

p = []
cd = {}
qList=[]
request_context = RequestContext(request)

if request_context.request.GET:
form = SearchForm(request_context.request.GET)
# print form
if form.is_valid():
cd = request_context.request.GET
projectId = cd['project']
app = cd['app']
if cd['days'] != '0':
daysAgo = '-' + cd['days']
else:
daysAgo = cd['days']
sdtime = cd['sdtime']
edtime = cd['edtime']

qList = ["Hello, I am X", "Hello, I am Y", "Hello, I am A", "Hello, I am B"]

for func in qList:
print func
t.sleep(10)
p.append(func)
context = {'query_output': p, 'id_project': projectId,
'app': app, 'pOutput': p}
return render(request, 'temp.html', context)

else:
form = SearchForm()
return render_to_response('search_form.html', {'form': form})

Thanks
crkatz

Stephen J. Butler

unread,
May 30, 2016, 6:52:12 PM5/30/16
to django...@googlegroups.com
What's your actual output vs. what's your desired output?

Also, the RequestContext is meant for the render() function. If you just want to access request.GET then you can do it directly (request is passed in as the first parameter to your view func). You also don't need to pre-assign qList and cd, as long as they are assigned before they are accessed you are OK.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/04067a15-d2cd-48d9-810c-8abe5760278c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

cr katz

unread,
May 30, 2016, 9:13:21 PM5/30/16
to Django users
Single request from the user gives the correct output as below. Please see the appended response further below when a user submits 2 requests from 2 different browsers on the same dev machine.

I don't want the users to see appended responses, rather, each request must return response as intended with in the request context.

Hello, I am A
Hello, I am X
Hello, I am B
Hello, I am Y


Appended response when user submits 2 requests simultaneously from 2 diff browsers:

Hello, I am A
Hello, I am X
Hello, I am B
Hello, I am Y
Hello, I am A
Hello, I am B

Stephen J. Butler

unread,
May 30, 2016, 9:32:47 PM5/30/16
to django...@googlegroups.com
I don't see anything in your pasted code that would cause that in the browser. Have you provided us a complete example that exhibits that behavior? Reduce your project down to the smallest set of code that exhibits the problem and then upload it somewhere we can see it.

cr katz

unread,
May 30, 2016, 10:16:56 PM5/30/16
to Django users
The code I shared is where the problem is. Here is the output once again from the server logs. It is evident that the second request is running into the first and appending the responses.

The text in BOLD 'Entered Search' is the entry point for each request. I suspect a problem with looping qList or appending 'p'.

Entered Search
<QueryDict: {u'project': [u'project1'], u'app': [u'app'], u'sdtime': [u'2016-05-30 00:00:00 UTC'], u'days': [u'0'], u'edtime': [u'2016-05-30 00:02:00 UTC']}>
Hello, I am X

Hello, I am Y
Hello, I am A
Entered Search
<QueryDict: {u'project': [u'project1'], u'app': [u'app'], u'sdtime': [u'2016-05-30 00:00:00 UTC'], u'days': [u'0'], u'edtime': [u'2016-05-30 00:02:00 UTC']}>

Hello, I am X
Hello, I am B
Hello, I am Y
[30/May/2016 22:00:35] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 893

Hello, I am A
Hello, I am B
[30/May/2016 22:00:58] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 1475

James Schneider

unread,
May 30, 2016, 10:34:54 PM5/30/16
to django...@googlegroups.com
On Mon, May 30, 2016 at 7:16 PM, cr katz <crka...@gmail.com> wrote:
The code I shared is where the problem is. Here is the output once again from the server logs. It is evident that the second request is running into the first and appending the responses.

The text in BOLD 'Entered Search' is the entry point for each request. I suspect a problem with looping qList or appending 'p'.

Entered Search
<QueryDict: {u'project': [u'project1'], u'app': [u'app'], u'sdtime': [u'2016-05-30 00:00:00 UTC'], u'days': [u'0'], u'edtime': [u'2016-05-30 00:02:00 UTC']}>
Hello, I am X
Hello, I am Y
Hello, I am A
Entered Search
<QueryDict: {u'project': [u'project1'], u'app': [u'app'], u'sdtime': [u'2016-05-30 00:00:00 UTC'], u'days': [u'0'], u'edtime': [u'2016-05-30 00:02:00 UTC']}>
Hello, I am X
Hello, I am B
Hello, I am Y
[30/May/2016 22:00:35] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 893
Hello, I am A
Hello, I am B
[30/May/2016 22:00:58] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 1475



The view code you posted will never provide the outputs you've listed here, no matter what the input arguments are.

Your view code will always output all of the elements in the qList variable (and consequently append() all of the values to p). You're not doing any filtering through the for loop where your print statement is located. I don't know how you are running this, but I'm assuming you've updated your view code since the OP, or the generalized modifications you made for the post are masking what is actually happening.

-James

Stephen J. Butler

unread,
May 30, 2016, 10:51:26 PM5/30/16
to django...@googlegroups.com
Ohhh... you're looking at the server logs? Those are never going to be sequential. Your requests operate in parallel. That's why you see interleaved outputs. Modify your print statement like this to see what's happening:

print "<Request: {0:08X}> {1}".format(id(request), func)

You should see the proper results, but for each individual request, not for the server process as a whole.

If you need sequential processing then you will have to use locks, either at the OS level or some shared resource (like the database).

cr katz

unread,
May 31, 2016, 10:40:59 AM5/31/16
to Django users
Thanks Stephen. As suggested, below is the output. 'p' is being overwritten before the first response is completely read and sent to the browser.

<Request: 039ADED0> Hello, I am X
<Request: 039ADED0> Hello, I am Y
<Request: 039ADED0> Hello, I am A

Entered Search
<QueryDict: {u'project': [u'project1'], u'app': [u'app'], u'sdtime': [u'2016-05-30 00:00:00 UTC'], u'days': [u'0'], u'edtime': [u'2016-05-30 00:02:00 UTC']}>

hd-www-stage

2016-05-30 00:00:00 UTC
<Request: 03A07A10> Hello, I am X
<Request: 039ADED0> Hello, I am B
<Request: 03A07A10> Hello, I am Y
[31/May/2016 10:25:15] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 932
<Request: 03A07A10> Hello, I am A
<Request: 03A07A10> Hello, I am B
[31/May/2016 10:25:37] "GET /search/?project=project1&app=app&days=0&sdtime=2016-05-30+00%3A00%3A00+UTC&edtime=2016-05-30+00%3A02%3A00+UTC HTTP/1.1" 200 932
        
On the browser, I see below in 1 request
Hello, I am X
Hello, I am B
Hello, I am Y     

2 simultaneous request prints as below on the browser:
 Hello, I am X
Hello, I am B
Hello, I am Y
Hello, I am A
Hello, I am B

Stephen J. Butler

unread,
May 31, 2016, 1:54:44 PM5/31/16
to django...@googlegroups.com
The code and output you've shown us don't make any sense. You're really going to have to provide a complete, minimal project.

Reply all
Reply to author
Forward
0 new messages