Some improvements to the commands.process function - better process naming and informational output.

This commit is contained in:
Daniel Folkinshteyn 2010-08-05 13:45:02 -04:00
parent 0c300162d8
commit 89cbc7efdf
3 changed files with 16 additions and 11 deletions

View File

@ -138,7 +138,7 @@ class String(callbacks.Plugin):
s = 'You probably don\'t want to match the empty string.' s = 'You probably don\'t want to match the empty string.'
irc.error(s) irc.error(s)
else: else:
v = commands.process(f, text, timeout=10) v = commands.process(f, text, timeout=10, pn=self.name(), cn='re')
irc.reply(v) irc.reply(v)
re = thread(wrap(re, [first('regexpMatcher', 'regexpReplacer'), re = thread(wrap(re, [first('regexpMatcher', 'regexpReplacer'),
'text'])) 'text']))

View File

@ -981,11 +981,11 @@ class CommandProcess(world.SupyProcess):
to run in processes. to run in processes.
""" """
def __init__(self, target=None, args=(), kwargs={}): def __init__(self, target=None, args=(), kwargs={}):
self.command = args[0] pn = kwargs.pop('pn', 'Unknown')
self.cb = target.im_self cn = kwargs.pop('cn', 'unknown')
procName = 'Process #%s (for %s.%s)' % (world.processesSpawned, procName = 'Process #%s (for %s.%s)' % (world.processesSpawned,
self.cb.name(), pn,
self.command) cn)
log.debug('Spawning process %s (args: %r)', procName, args) log.debug('Spawning process %s (args: %r)', procName, args)
self.__parent = super(CommandProcess, self) self.__parent = super(CommandProcess, self)
self.__parent.__init__(target=target, name=procName, self.__parent.__init__(target=target, name=procName,

View File

@ -70,22 +70,27 @@ def thread(f):
return utils.python.changeFunctionName(newf, f.func_name, f.__doc__) return utils.python.changeFunctionName(newf, f.func_name, f.__doc__)
def process(f, *args, **kwargs): def process(f, *args, **kwargs):
"""Runs a function in a subprocess. """Runs a function <f> in a subprocess.
Takes an extra timeout argument, which, if supplied, limits the length
of execution of target function to <timeout> seconds.""" Several extra keyword arguments can be supplied.
timeout = kwargs.pop('timeout') <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() q = multiprocessing.Queue()
def newf(f, q, *args, **kwargs): def newf(f, q, *args, **kwargs):
r = f(*args, **kwargs) r = f(*args, **kwargs)
q.put(r) q.put(r)
targetArgs = (f, q,) + args targetArgs = (f, q,) + args
p = world.SupyProcess(target=newf, p = callbacks.CommandProcess(target=newf,
args=targetArgs, kwargs=kwargs) args=targetArgs, kwargs=kwargs)
p.start() p.start()
p.join(timeout) p.join(timeout)
if p.is_alive(): if p.is_alive():
p.terminate() p.terminate()
q.put("Function call aborted due to timeout.") q.put("%s aborted due to timeout." % (p.name,))
try: try:
v = q.get(block=False) v = q.get(block=False)
except Queue.Empty: except Queue.Empty: