Have predict.py fork for itself, and use an alarm to prevent hung processes

pull/73/head
Daniel Richman 2011-04-30 10:56:12 +01:00
rodzic d65d54791b
commit 9ac0aa0e6e
2 zmienionych plików z 39 dodań i 6 usunięć

Wyświetl plik

@ -70,6 +70,13 @@ def main():
# Set up our command line options
parser = optparse.OptionParser()
parser.add_option('-d', '--cd', dest='directory',
help='change to, and run in, directory DIR',
metavar='DIR')
parser.add_option('--fork', dest='fork', action="store_true",
help='detach the process and run in the background')
parser.add_option('--alarm', dest='alarm', action="store_true",
help='setup an alarm for 10 minutes time to prevent hung processes')
parser.add_option('-t', '--timestamp', dest='timestamp',
help='search for dataset covering the POSIX timestamp TIME \t[default: now]',
metavar='TIME', type='int',
@ -133,6 +140,15 @@ def main():
log.error('Exactly one positional argument should be supplied (uuid).')
sys.exit(1)
if options.directory:
os.chdir(options.directory)
if options.fork:
detach_process()
if options.alarm:
setup_alarm()
uuid = args[0]
uuid_path = options.preds_path + "/" + uuid + "/"
@ -518,6 +534,27 @@ def dataset_for_time(time, hd):
raise RuntimeError('Could not find appropriate dataset.')
def detach_process():
# Fork
if os.fork() > 0:
os._exit(0)
# Detach
os.setsid()
null_fd = os.open(os.devnull, os.O_RDWR)
for s in [sys.stdin, sys.stdout, sys.stderr]:
os.dup2(null_fd, s.fileno())
# Fork
if os.fork() > 0:
os._exit(0)
def setup_alarm():
# Prevent hung download:
import signal
signal.alarm(600)
# If this is being run from the interpreter, run the main function.
if __name__ == '__main__':
main()

Wyświetl plik

@ -122,17 +122,13 @@ function runPred($pred_model) {
$predictor_lat = number_format($pred_model['lat'], 0);
$predictor_lon = number_format($pred_model['lon'], 0);
// use `at` to automatically background the task
$ph = popen("at now", "w");
$sh = "./predict.py -v --latdelta="
$sh = ROOT . "/predict.py --cd=" . ROOT . " --fork --alarm -v --latdelta="
.$pred_model['delta_lat']." --londelta=".$pred_model['delta_lon']
." -p1 -f5 -t ".$pred_model['timestamp']
." --lat=".$predictor_lat." --lon=".$predictor_lon." " . $use_hd
. $pred_model['uuid'];
if (DEBUG) shell_exec("echo " . $sh . " > " . AT_LOG);
fwrite($ph, "cd " . ROOT . " && " . $sh );
fclose($ph);
shell_exec($sh);
}
// Use PHP's mkdir() to create a directory for the prediction data using