CGI and long running job

Tim Roberts timr at probo.com
Sat Dec 3 20:51:02 EST 2005


merry.sailor at gmail.com wrote:

>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)
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list