[Tutor] cgi script: how to continue a process in the backgrou nd and return from cgi script

Beilin Zhang bzhang at sangamo.com
Fri Feb 10 20:10:28 CET 2006


I think you need to close the IO streams before forking the child process.
Something like 
    sys.stdout.flush()
    sys.stdout.close()  
    sys.stdin.close()
    sys.stderr.close()
    os.close(0)
    os.close(1)
    os.close(2)
This is probably not the best way, but it works for me.

Beilin Zhang

-----Original Message-----
From: Moritz Lennert [mailto:mlennert at club.worldonline.be]
Sent: Friday, February 10, 2006 8:03 AM
To: tutor at python.org
Subject: [Tutor] cgi script: how to continue a process in the background
and return from cgi script


Hello,

I need some pointers in the right direction for the following problem:

I have a cgi script which reads in some form elements, uses them to 
compose an SQL query, sends that query to the postgresql backend, writes 
the results into a temporary file and sends a mail to the user with the 
link to the file.

The main block of the program is very simple

formulaire = cgi.FieldStorage(keep_blank_values=1)
adresse=formulaire["email1"].value
if adresse == "":
    print_error_adresse()
else:
    requete=compose_req(formulaire) #function composing the query
    print_info_mail(adresse, requete) #function printing a web page 
telling the user that the query is ongoing and that she will be advised 
by email when it is done
    results=req(requete, 91) #function launching the query
    fname=fichier_resultats(results) #function writing the results to a file
    if(fname):
      envoi_res(fname, requete, adresse) #function sending the email to 
the user


The problem I have is that the process obviously remains active 
throughout the entire program, including the query, and that I, 
therefore, sometimes get timeout problems with apache.

So, what I would like to do is to go through all the steps until the 
printing of the web page and then somehow fork the process of querying 
so that the cgi script terminates and the querying, result file writing 
and email sending are done in the background.

I have no experience with either forking or threading, but the way I 
understand them any child processes die when the parent dies.

I have tried the recipe in 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 which is 
supposed to fork a process as daemon, thus decoupling it from the 
calling terminal, but I don't seem to get it to work (I get one defunct 
process and the child process, but the timeout problem remains). I am 
also not sure this is actually what I need (I don't know if what I 
describe above is "decoupling from the calling terminal").

So, could some give me a pointer to possible solutions ? Do I have to 
make the last part of my program a seperate program and go through a 
system call ?

Thank you !

Moritz
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list