Skip to content

Instantly share code, notes, and snippets.

@tinnet
Created February 26, 2012 17:50
Show Gist options
  • Save tinnet/1917945 to your computer and use it in GitHub Desktop.
Save tinnet/1917945 to your computer and use it in GitHub Desktop.
runs another python module for a maximum time
import imp
import os
import sys
import threading
USAGE = "python %s YOUR_MODULE TIMEOUT_IN_SECONDS arg1 arg2 arg3" % sys.argv[0]
class ModuleRunner(threading.Thread):
def __init__(self, module_name):
threading.Thread.__init__(self)
# strip the ".py" from the modulename (if the user gave it)
self.module_name = os.path.splitext(module_name)[0]
def run(self, *args, **kwargs):
file, pathname, description = imp.find_module(self.module_name)
try:
# attn: multiple threads + global sys.argv = problems!
sys.argv[0] = pathname
imp.load_module("__main__", file, pathname, description)
finally:
file.close()
@classmethod
def runModule(cls, module_name):
worker = ModuleRunner(module_name)
worker.start()
return worker
@classmethod
def runModuleWithTimeout(cls, module_name, timeout):
worker = ModuleRunner(module_name)
worker.start()
worker.join(timeout)
if worker.is_alive():
worker._Thread__stop()
raise Exception("module took longer than %f seconds, worker was killed" % timeout)
def main():
if len(sys.argv) < 3:
print "missing arguments, usage:", USAGE
return 1
max_seconds = float(sys.argv[2])
del sys.argv[2]
module = sys.argv[1]
del sys.argv[1]
try:
ModuleRunner.runModuleWithTimeout(module, max_seconds)
except Exception as e:
print >> sys.stderr, "Exception:", e
return 1
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment