It's just magic, and I hate it ...

Mitchell Morris mgm at unpkhswm04.bscc.bls.com
Mon Jun 21 13:04:44 EDT 1999


Following is a piece of Python that is making me pull out my hair. The idea
was to have a long-running process started by the user (via browser), and
have the results trickle back in without timing out the request. My strategy
was to define a per-connection file to hold results, fork off a child to
generate results and put them into the file, and have the parent return a
redirect to the results file. That way everyone is satisfied.

It works if I run it from the command line as myself. It works if I run it
from the command line as user 'WWW' group 'WWW' which is how the server will
see it. The results file is created, the redirect is printed and a shell
prompt returns, and the child has forked off and in fact updates the file
every so often until it finishes at which time it replaces the contents with
the simulated results.

When run from the browser, however, the redirect doesn't happen until the
child finishes. To recap, this is exactly the situation I need to avoid.

Please help.

+Mitchell



#! /usr/bin/env python

import sys
import cgi
import time
import os
import string
import socket

form = cgi.FieldStorage()
if form.has_key("title"):
	hostname = socket.gethostbyaddr(socket.gethostname())[0]
	uniq = hex(os.getpid())[2:] + hex(int(time.time()))[2:]
	addr = "http://%s/~mgm/results/%s.html" % (hostname, uniq)
	fn = "/home/mgm/public_html/results/%s.html" % uniq

	# Generate the results file
	open(fn, "w").write("""<HTML>
<HEAD>
<META HTTP-EQUIV=REFRESH CONTENT=5>
<TITLE>Results for [%s]</TITLE>
</HEAD>
<BODY>
<P>Beginning search at %s:
</BODY>
</HTML>
""" % (form["title"].value, time.asctime(time.localtime(time.time()))))

	# Parent returns a redirect, child pretends to work for a while
	# accumulating results
	if os.fork() == 0:
		for i in (1, 2, 3):
			txt = open(fn, 'r').readlines()
			txt.insert(len(txt)-2, "<P>Still searching\n")
			open(fn, 'w').write(string.join(txt, ''))
			time.sleep(10)
		open(fn, 'w').write("""<HTML>
<HEAD>
<TITLE>Finished</TITLE>
</HEAD>
<BODY>
<H1>Finished</H1>
<P>If you had results, they'd be here
</BODY>
</HTML>
""")
	else:
		print "Status: 302 Moved"
		print "Location:", addr
else:
	print """Content-type: text/html

<HTML>
<HEAD>
<TITLE>Sample Form</TITLE>
</HEAD>
<BODY>
<P>
<H1>Sample Form</H1>
<FORM METHOD="POST">
Title: <INPUT TYPE="TEXT" NAME="title"><P>
<INPUT TYPE="SUBMIT" NAME="submit">
</FORM>
</BODY>
</HTML>"""



-- 
Mitchell Morris

A man said his credit card was stolen but he decided not to
report it because the thief was spending less than his wife
did.




More information about the Python-list mailing list