[Pythonmac-SIG] thread blocking problem?

William McLendon txagcs98 at hotmail.com
Thu Oct 16 14:50:23 EDT 2003


This has me quite perplexed.  I'm trying to make a little app that uses a 
basic http server that can launch a series of scripts and still run while 
the scripts are running.  I'm attaching 2 source files that can replicate 
what's happening in my bigger script:

I put these two files (myhttpd.py, execute.py) in one directory and run 
myhttpd.py.  Load http://127.0.0.1:2000/ in a browser and hit the execute 
button.  This should then kick off execute.py in a different thread?  I've 
used a variety of different commands to run it (spawnv in this example) but 
the result is the same.  The big problem is that the main thread running the 
http server seems to block till after execute.py is done.  What is causing 
the blocking here?

you can watch the progress of execute.py by doing tail -f out.txt in another 
window... the blocking should show up then.

I've even tried making execute.py daemonize itself but the blocking still 
happens.

#!/usr/bin/env python2.2
# myhttpd.py

import os
import time
import BaseHTTPServer

class httpRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        server_version='test/1.0'
        def do_POST(self):
                if self.path=='/execute':
                        self.send_response(200,'OK')
                        self.end_headers()
                        self.wfile.write("<html><head></head><body>\n"
		self.wfile.write("<h1>done</h1></body></html>\n")
                        pid = os.fork()
                        if pid == 0:
                                os.spawnv(os.P_NOWAIT,'execute.py', [''])
                                os._exit(0)
        def do_GET(self):
                if self.path=='/':
                        self.send_response(200,'OK')
                        self.end_headers()
                        self.wfile.write('<html><head></head><body>\n')
                        self.wfile.write('<form name=execute method=post ')
		self.wfile.write('action="http://127.0.0.1:2000/execute">\n')
                        self.wfile.write('  <input type="submit" 
name="execute" value="execute">\n')
                        self.wfile.write('</form></body></html>\n')

if __name__ == '__main__':
        server = BaseHTTPServer.HTTPServer(('127.0.0.1',2000), 
httpRequestHandler)
        while 1:
                server.handle_request()


#!/usr/bin/env python
# execute.py
import os
import time
ofp = open('out.txt','a')
n = 10
for i in range(n):
	time.sleep(3)
	ofp.write((n-i+1)*' ')
	ofp.write((2*i+1)*'.'+'\n')
	ofp.flush()
for i in range(2):
	ofp.write((n)*' ')
	ofp.write('...\n')
	ofp.flush()
ofp.close()

I'm running this with python 2.2 on OS X 10.2.6 -- any help to get this to 
execute without blocking would be greatly appreciated.

Thanks,
  -William

_________________________________________________________________
See when your friends are online with MSN Messenger 6.0. Download it now 
FREE! http://msnmessenger-download.com




More information about the Pythonmac-SIG mailing list