Skip to content

Commit

Permalink
src/commands.py: make subprocesses raise an error on timeout, rather …
Browse files Browse the repository at this point in the history
…than return a string

Conflicts:

	src/commands.py
  • Loading branch information
Daniel Folkinshteyn authored and progval committed Aug 12, 2011
1 parent d85cbd2 commit 3526d5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/commands.py
Expand Up @@ -69,6 +69,43 @@ def newf(self, irc, msg, args, *L, **kwargs):
f(self, irc, msg, args, *L, **kwargs)
return utils.python.changeFunctionName(newf, f.func_name, f.__doc__)

class ProcessTimeoutError(Exception):
"""Gets raised when a process is killed due to timeout."""
pass

def process(f, *args, **kwargs):
"""Runs a function <f> in a subprocess.
Several extra keyword arguments can be supplied.
<pn>, the pluginname, and <cn>, the command name, are strings used to
create the process name, for identification purposes.
<timeout>, if supplied, limits the length of execution of target
function to <timeout> seconds."""
timeout = kwargs.pop('timeout', None)

q = multiprocessing.Queue()
def newf(f, q, *args, **kwargs):
try:
r = f(*args, **kwargs)
q.put(r)
except Exception as e:
q.put(e)
targetArgs = (f, q,) + args
p = callbacks.CommandProcess(target=newf,
args=targetArgs, kwargs=kwargs)
p.start()
p.join(timeout)
if p.is_alive():
p.terminate()
raise ProcessTimeoutError, "%s aborted due to timeout." % (p.name,)
try:
v = q.get(block=False)
except Queue.Empty:
v = "Nothing returned."
if isinstance(v, Exception):
v = "Error: " + str(v)
return v

class UrlSnarfThread(world.SupyThread):
def __init__(self, *args, **kwargs):
assert 'url' in kwargs
Expand Down
2 changes: 1 addition & 1 deletion src/version.py
@@ -1,3 +1,3 @@
"""stick the various versioning attributes in here, so we only have to change
them once."""
version = '0.83.4.1+limnoria (2011-08-12T18:51:40+0200)'
version = '0.83.4.1+limnoria (2011-08-13T01:53:58+0200)'

0 comments on commit 3526d5d

Please sign in to comment.