[Pythonmac-SIG] thread blocking problem?

Bob Ippolito bob at redivi.com
Fri Oct 17 15:00:52 EDT 2003


On Friday, Oct 17, 2003, at 14:14 America/New_York, William McLendon 
wrote:

> I'm pretty new with this...  What the differences in these are?  
> Critical issues, etc?
>
> Is Python's fork()ing incomplete?
>
> Any workarounds?
>
>> From: John P Speno <speno at isc.upenn.edu>
>> To: pythonmac-sig at python.org
>> Subject: Re: [Pythonmac-SIG] thread blocking problem?
>> Date: Fri, 17 Oct 2003 11:14:56 -0400
>>
>> On Thu, Oct 16, 2003 at 12:50:23PM -0600, William McLendon wrote:
>> > if __name__ == '__main__':
>> >        server = BaseHTTPServer.HTTPServer(('127.0.0.1',2000),
>>
>> Could it be that BaseHTTPServer.HTTPServer is a SocketServer.TCPServer
>> and not a SocketServer.ThreadingTCPServer or 
>> SocketServer.ForkingTCPServer?

First of all, you said you're using: "python 2.2 on OS X 10.2.6".  This 
sounds like the comes-with-Jaguar version of Python.  It's buggy, 
unstable, and missing a few key things.  Install MacPython 2.3.  OS X 
10.3 comes with (most of) MacPython 2.3, so you could just wait a week 
and upgrade your OS instead.

Second, I suggest against using BaseHTTPServer.  Twisted ( 
http://www.twistedmatrix.com/ ) is much more efficient (with multiple 
concurrent requests) than any of the BaseHTTPServer implementations, 
integrates with whatever networking you want (SMTP, IMAP, AIM, IRC, 
DNS, .. the list is huge), and is based on an async model rather than a 
threading/forking model so it's faster under Python (due to the GIL and 
such).  Below is your example (myhttpd.py, the execute.py isn't 
changed, of course) naively translated to Twisted.  Please read Twisted 
documentation if you decide to use it.  The Twisted mailing list and 
the #twisted IRC channel on irc.freenode.net are also very helpful when 
you have questions that you can't answer yourself from the 
documentation.

#!/usr/bin/env python
# myhttpd.py

from twisted.web.resource import Resource
from twisted.web import server
from twisted.internet import utils
from twisted.python import log

class ExecuteResource(Resource):
     def render(self, req):
         if not req.method == 'POST':
             return ("<html><body><pre>\n"
                     "You may only POST to this resource\n"
                     "</pre></body></html>\n")
         # this creates a deferred that will callback with
         # error or output.. the lambda will just trap+ignore
         # an error or output
         utils.getProcessOutput("execute.py").addBoth(lambda result: 
None)
         return ("<html><body><pre>\n"
                 "Your process is running in the background\n"
                 "</pre></body></html>\n")

class RootResource(Resource):
     def render(self, req):
         return ("""<html>\n<head></head>\n<body>\n"""
                 """<form name="execute" method="post" 
action="execute">\n"""
                 """<input type="submit" name="execute" 
value="execute">\n"""
                 """</form></body></html>\n""")


if __name__ == '__main__':
     import sys
     from twisted.internet import reactor
     log.startLogging(sys.stdout)
     root = Resource()
     root.putChild('execute', ExecuteResource())
     root.putChild('', RootResource())
     site = server.Site(root)
     reactor.listenTCP(2000, site)
     reactor.run()




More information about the Pythonmac-SIG mailing list