CGI and long running job

M

merry.sailor

Hello,
I am using python and CGI to initiate a long running simulation (up to
5h long) on a remote machine. The main idea is that I use a form, enter
the parameters and a CGI scripts start the simulation using these
parameters. The structure of the script is:

1. Read paremeters
2. Display some information
3. Start the simulation

For step 3 I use either os.system or os.popen(2). The problem is that
the web server does not send the information of step 2 back to the
browser, unless step 3 is completed. The browser simply waits for a
response, without displaying anything. How can I just read the params,
display the info I want, start the simulation and then terminate either
the script or at least the connection to the browser without having to
wait for the simulation to finish?

I am using activestate python 2.4, Apache and WinXP.

Thanks a lot for your help.
 
J

jmdeschamps

Hello,
I am using python and CGI to initiate a long running simulation (up to
5h long) on a remote machine. The main idea is that I use a form, enter
the parameters and a CGI scripts start the simulation using these
parameters. The structure of the script is:

1. Read paremeters
2. Display some information
3. Start the simulation

For step 3 I use either os.system or os.popen(2). The problem is that
the web server does not send the information of step 2 back to the
browser, unless step 3 is completed. The browser simply waits for a
response, without displaying anything. How can I just read the params,
display the info I want, start the simulation and then terminate either
the script or at least the connection to the browser without having to
wait for the simulation to finish?

I am using activestate python 2.4, Apache and WinXP.

Thanks a lot for your help.

Maybe you could use 'os.spawn' variants with the P_NOWAIT parameter...
(not sure of the syntax here, it's in the doc)
Good luck!
 
M

merry.sailor

Maybe you could use 'os.spawn' variants with the P_NOWAIT parameter...
(not sure of the syntax here, it's in the doc)
Good luck!

Thanks for answering. I tried that but it didn't work. Nothing is sent
back to the browser until the spawned process has ended. This is true
even if the spawn command (or any other similar command) is the last
statement of the script.

Is there some way to send back to the browser whatever info I need,
close the connection and then continue executing the commands I need,
using the same script?
 
M

Mardy

Le die Fri, 02 Dec 2005 20:35:52 -0800, merry.sailor ha scribite:
For step 3 I use either os.system or os.popen(2). The problem is that
the web server does not send the information of step 2 back to the
browser, unless step 3 is completed. The browser simply waits for a
response, without displaying anything. How can I just read the params,
display the info I want, start the simulation and then terminate either
the script or at least the connection to the browser without having to
wait for the simulation to finish?

Sounds weird. What if you connect to the webserver via telnet? Do you get
any output?
you could try to call sys.stdout.flush() in the CGI, but I never had the
need to do that.
Maybe it's just a browser's problem?
 
M

merry.sailor

Hello Mardy,
thanks a lot for your help. I found the problem. Your suggestion made
me look into some things I haven't thought before :). I don't know if
it is a browser/server/mine fault. The problem was that I was sending
text/plain and a txt file. I soon as I started sending back to the
browser text/html and an html file, I started getting the things I
wanted on screen, but the browser still waited for the script to
terminate, which resulted in a timeout. After I also closed stdout,
everything worked fine. The strange thing is that I tried closing
stdout with the plain text before, but it didn't work.
 
T

Tim Roberts

Hello,
I am using python and CGI to initiate a long running simulation (up to
5h long) on a remote machine. The main idea is that I use a form, enter
the parameters and a CGI scripts start the simulation using these
parameters. The structure of the script is:

1. Read paremeters
2. Display some information
3. Start the simulation

For step 3 I use either os.system or os.popen(2). The problem is that
the web server does not send the information of step 2 back to the
browser, unless step 3 is completed. The browser simply waits for a
response, without displaying anything. How can I just read the params,
display the info I want, start the simulation and then terminate either
the script or at least the connection to the browser without having to
wait for the simulation to finish?

You need to close stdout, which closes the socket and allows the browser to
finish. This is what I use:

# We flush stdout, fork, then close stdout. This closes the HTTP socket,
# allowing the browser to continue while we munge with sendmail.

sys.stdout.flush()
if os.fork(): return
fw = open('/dev/null','w')
os.dup2(fw.fileno(),1)
os.dup2(fw.fileno(),2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top