Running long script in the background

Thomas Guettler guettli.usenet at thomas-guettler.de
Tue Feb 6 10:37:24 EST 2007


wattersmt at gmail.com wrote:

> Hello,
>
> I am trying to write a python cgi that calls a script over ssh, the
> problem is the script takes a very long time to execute so Apache
> makes the CGI time out and I never see any output.  The script is set
> to print a progress report to stdout every 3 seconds but I never see
> any output until the child process is killed.
>
> Here's what I have in my python script:
>
> command = "ssh -l root %s /scripts/xen/xen-create-win-vps1.sh %s" %
> (host, domuname)
> output = os.popen(command)
> for line in output:
>    print line.strip()

try sys.stdout.flush() after every print.

Or try something like this:
import sys, time

class FlushFile:
    def __init__(self, fd):
        self.fd = fd
    def flush(self):
        self.fd.flush()
    def write(self, str):
        self.fd.write(str)
        self.fd.flush()

oldstdout = sys.stdout
sys.stdout = FlushFile(sys.stdout)

for i in range(5):
    print "Hello",
    time.sleep(0.5)
print 

-- 
Thomas Güttler, http://www.thomas-guettler.de/ http://www.tbz-pariv.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: niemand.leermann at thomas-guettler.de




More information about the Python-list mailing list