Running long script in the background
petercable at gmail.com
petercable at gmail.com
Wed Feb 7 02:13:30 EST 2007
On Feb 6, 5:26 am, "watter... at gmail.com" <watter... 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.
>
<snip>
>
> Does anybody know a way to make output show in real time?
Try this:
<code>
# test.py
import os
import sys
import time
def command():
for x in range(5):
print x
sys.stdout.flush()
time.sleep(1)
def main():
command = 'python -c "import test; test.command()"'
print 'running: %s' % command
output = os.popen(command, 'r', 1)
while True:
line = output.readline()
if line == '':
break
sys.stdout.write(line)
sys.stdout.flush()
if __name__ == '__main__':
main()
</code>
The problem is with using the file-like object returned by popen as an
iterator. It will block until the child process is killed, so just
iterate across it manually.
Pete
More information about the Python-list
mailing list