0

I started with a simple Python 2.5 app engine app and migrated it to Python 2.7 in hopes of taking advantage of its multithreaded abilities. After migrating, I noticed that webapp2.RequestHandler instances are all being called from the Main Thread.

I have an AJAX client firing up multiple asynchronous requests. One of the requests I'd like to respond only when certain event occurs on server side. Let's just say that event sleeps for 10 seconds for now. The problem is that sleep occurs in the Main Thread and occupies the thread before processing my second ASYNC request from AJAX. What am I missing?

Here's a stack trace:

PyDevDebug [PyDev Google App Run] dev_appserver.py
MainThread - pid4276_seq4
post [test1.py:53]
dispatch [webapp2.py:570]
call [webapp2.py:1102]
default_dispatcher [webapp2.py:1278]
call [webapp2.py:1529]
Handle [wsgi.py:223]
HandleRequest [wsgi.py:298] HandleRequest [runtime.py:151]
ExecutePy27Handler [dev_appserver.py:1525]
ExecuteCGI [dev_appserver.py:1701]
Dispatch [dev_appserver.py:1803]
Dispatch [dev_appserver.py:719] _Dispatch [dev_appserver.py:2870]
_HandleRequest [dev_appserver.py:3001]
do_POST [dev_appserver.py:2794] handle_one_request [BaseHTTPServer.py:328]
handle [BaseHTTPServer.py:340]
init [SocketServer.py:638]
init [dev_appserver.py:2780]
finish_request [SocketServer.py:323]
process_request [SocketServer.py:310]
_handle_request_noblock [SocketServer.py:284]
handle_request [dev_appserver.py:3991]
serve_forever [dev_appserver.py:4028]
main [dev_appserver_main.py:721]
[dev_appserver_main.py:747]
run_file [dev_appserver.py:167] [dev_appserver.py:171] run [pydevd.py:1090]
[pydevd.py:1397]
Thread-4 - pid4276_seq5 dev_appserver.py

mip
  • 8,355
  • 6
  • 53
  • 72
Henry
  • 133
  • 1
  • 2
  • 4
  • 1
    are you deployed or local? if local, try it when deployed. threads tend not to be multithreaded when local. – Paul Collingwood Jan 30 '13 at 18:54
  • That was it! I spent hours trying to find out why it wasn't working, and deployed it as-is, and it works! Thanks! Any idea why it doesn't work locally or how I can make it work? Hard to develop locally if it doesn't behave correctly – Henry Jan 30 '13 at 19:09
  • lot's of things don't work locally, or rather the don't work as they will behave when deployed. Well, not that many things really but if what you are trying to do is one of them (I spend alot of time on addressing backends at one point to no avail, it 'just works' when deployed) it's a pain until you realize. In general, assume parallel activity will be emulated serially locally. I got used to working in a "half live, half local" environment with *lots* of debug logging. – Paul Collingwood Jan 30 '13 at 19:23
  • Got it, that's very useful to know that I can't rely on it then. I'll have to come up with a debugging strategy when deployed :) I'm not seeing how to accept your original answer btw. – Henry Jan 30 '13 at 19:28
  • I've added a bit more as an actual answer rather then a comment now. – Paul Collingwood Jan 30 '13 at 19:29

1 Answers1

0

It seems that in the dev (local) server parallel threads are not executed in parallel, but serial.

There is an "experimental dev server" here which you just reminded me of:

An experimental new development server for Google App Engine.

Multithreaded serving for better performance for complex applications and more correct semantics e.g. accessing your own application through urlfetch no longer deadlocks.

That might solve it locally, but I've not tried it personally.

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36