diff options
author | 2018-05-27 12:02:02 +0200 | |
---|---|---|
committer | 2018-05-27 12:02:02 +0200 | |
commit | e63fd39cb7e0de4fdbfc20ec9e34c6dfedad8fb5 (patch) | |
tree | 0eeb1e1e2e39cb018d451c904655d2f48bf35c65 | |
parent | now also captures stderr (diff) |
kill long running processes after x seconds
-rw-r--r-- | snekbox.py | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -2,6 +2,8 @@ import sys import io import json import multiprocessing +import threading +import time from logs import log from rmq import Rmq @@ -54,12 +56,26 @@ def execute(body): exchange=snekid) exit(0) +def stopwatch(process): + log.debug(f"10 second timer started for process {process.pid}") + for _ in range(10): + time.sleep(1) + if not process.is_alive(): + log.debug(f"Clean exit on process {process.pid}") + exit(0) + + process.terminate() + log.debug(f"Rerminated process {process.pid} forcefully") def message_handler(ch, method, properties, body, thread_ws=None): p = multiprocessing.Process(target=execute, args=(body,)) p.daemon = True p.start() + t = threading.Thread(target=stopwatch, args=(p,)) + t.daemon = True + t.start() + ch.basic_ack(delivery_tag=method.delivery_tag) |