I've noticed some behavioral weirdness in my web app that I can
reproduce with the basic hello app. The problem in that the mainline
routine gets run twice: first by the main thread and then once again
by the first thread that handles a request. Why does it do this? It
only happens when the code in run from a file. If you paste the code
into an interpreter, it doesn't happen.
the code follows. After that is the captured output when handling
five requests. Note that the print line happens twice, once by the
main thread and once by Thread-1
A solution appears to be moving the "if __name__ ..." line up to the
top. I'm just curious as to why this is necessary.
-lars
---------------------------------------------
#!/usr/bin/python
import web
import threading
print '%s says, "this should only happen once"' %
threading.currentThread().getName()
urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals())
class hello:
def GET(self, name):
if not name:
name = 'World'
print "%s handled this one." %
threading.currentThread().getName()
return 'Hello, ' + name + '!'
if __name__ == "__main__":
app.run()
---------------------------------------------
Captured output:
MainThread says, "this should only happen once"
http://0.0.0.0:8080/
CP WSGIServer Thread-1 says, "this should only happen once"
CP WSGIServer Thread-1 handled this one.
127.0.0.1:60241 - - [28/Jul/2010 11:48:37] "HTTP/1.1 GET /hello" - 200
OK
CP WSGIServer Thread-2 handled this one.
127.0.0.1:60243 - - [28/Jul/2010 11:48:44] "HTTP/1.1 GET /hello" - 200
OK
CP WSGIServer Thread-3 handled this one.
127.0.0.1:60244 - - [28/Jul/2010 11:48:45] "HTTP/1.1 GET /hello" - 200
OK
CP WSGIServer Thread-4 handled this one.
127.0.0.1:60245 - - [28/Jul/2010 11:48:45] "HTTP/1.1 GET /hello" - 200
OK
CP WSGIServer Thread-5 handled this one.
127.0.0.1:60247 - - [28/Jul/2010 11:48:46] "HTTP/1.1 GET /hello" - 200
OK
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.