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

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
Daniel Folkinshteyn 2010-08-05 13:45:02 -04:00 committed by James McCoy
parent a2985c37d6
commit 7f98aa7105
3 changed files with 16 additions and 11 deletions

View File

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

View File

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

View File

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