简体   繁体   中英

In python 2.7 using CGI, how to check whether forked process completed

So I have a set of python scripts. In an attempt to make a simple GUI, I have been combining html and CGI. So far so good. However, one of my scripts takes a long time to complete (>2 hours). So obviously, when I run this on my server (localhost on mac) I get a "gateway timeout error". I was reading about forking the sub process, and checking whether the process completed.
This is what I came up with, but it isn't working :(.

import  os, time
#upstream stuff happening as part of main script
pid=os.fork()
    if pid==0: 
        os.system("external_program") #this program will make "text.txt" as output
        exit()

while os.stat(""text.txt").st_size == 0: # check whether text.txt has been written, if not print "processing" and continue to wait
    print "processing"
    sys.stdout.flush()
    time.sleep(300)
#downstream stuff happening

As alwas, any help is appreciated

Did you try this one:

import os
processing = len(os.popen('ps aux | grep yourscript.py').readlines()) > 2

It tells you if your script is still running (returns boolean value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM