Made sure to return our reply messages, changed some threading stuff, etc.

This commit is contained in:
Jeremy Fincher 2004-09-30 04:13:08 +00:00
parent 4ad7fd2504
commit 69dce4761d
2 changed files with 43 additions and 21 deletions

View File

@ -51,7 +51,6 @@ import getopt
import string import string
import inspect import inspect
import operator import operator
import threading
from cStringIO import StringIO from cStringIO import StringIO
from itertools import imap, ifilter from itertools import imap, ifilter
@ -706,7 +705,8 @@ class IrcObjectProxy(RichReplyMethods):
else: else:
cb = cbs[0] cb = cbs[0]
del self.args[0] # Remove the command. del self.args[0] # Remove the command.
if cb.threaded or conf.supybot.debug.threadAllCommands(): if world.isMainThread() and \
(cb.threaded or conf.supybot.debug.threadAllCommands()):
t = CommandThread(target=self._callCommand, t = CommandThread(target=self._callCommand,
args=(command, cb)) args=(command, cb))
t.start() t.start()
@ -789,21 +789,24 @@ class IrcObjectProxy(RichReplyMethods):
# action implies noLengthCheck, which has already been # action implies noLengthCheck, which has already been
# handled. Let's stick an assert in here just in case. # handled. Let's stick an assert in here just in case.
assert not self.action assert not self.action
self.irc.queueMsg(reply(msg, s, to=self.to, m = reply(msg, s, to=self.to,
notice=self.notice, notice=self.notice,
private=self.private, private=self.private,
prefixName=self.prefixName)) prefixName=self.prefixName)
return self.irc.queueMsg(m)
return m
msgs = ircutils.wrap(s, allowedLength-30) # -30 is for nick: msgs = ircutils.wrap(s, allowedLength-30) # -30 is for nick:
msgs.reverse() msgs.reverse()
instant = conf.supybot.reply.mores.instant() instant = conf.supybot.reply.mores.instant()
while instant > 1 and msgs: while instant > 1 and msgs:
instant -= 1 instant -= 1
response = msgs.pop() response = msgs.pop()
self.irc.queueMsg(reply(msg, response, to=self.to, m = reply(msg, response, to=self.to,
notice=self.notice, notice=self.notice,
private=self.private, private=self.private,
prefixName=self.prefixName)) prefixName=self.prefixName)
self.irc.queueMsg(m)
return m
if not msgs: if not msgs:
return return
response = msgs.pop() response = msgs.pop()
@ -823,11 +826,13 @@ class IrcObjectProxy(RichReplyMethods):
public = ircutils.isChannel(msg.args[0]) public = ircutils.isChannel(msg.args[0])
private = self.private or not public private = self.private or not public
Privmsg._mores[msg.nick] = (private, msgs) Privmsg._mores[msg.nick] = (private, msgs)
self.irc.queueMsg(reply(msg, response, to=self.to, m = reply(msg, response, to=self.to,
action=self.action, action=self.action,
notice=self.notice, notice=self.notice,
private=self.private, private=self.private,
prefixName=self.prefixName)) prefixName=self.prefixName)
self.irc.queueMsg(m)
return m
finally: finally:
self._resetReplyAttributes() self._resetReplyAttributes()
else: else:
@ -866,26 +871,26 @@ class IrcObjectProxy(RichReplyMethods):
return getattr(self.irc, attr) return getattr(self.irc, attr)
class CommandThread(threading.Thread): class CommandThread(world.SupyThread):
"""Just does some extra logging and error-recovery for commands that need """Just does some extra logging and error-recovery for commands that need
to run in threads. to run in threads.
""" """
def __init__(self, target=None, args=(), kwargs={}): def __init__(self, target=None, args=(), kwargs={}):
(self.name, self.cb) = args (self.name, self.cb) = args
self.__parent = super(CommandThread, self)
self.command = self.cb.getCommand(self.name) self.command = self.cb.getCommand(self.name)
world.threadsSpawned += 1
threadName = 'Thread #%s (for %s.%s)' % (world.threadsSpawned, threadName = 'Thread #%s (for %s.%s)' % (world.threadsSpawned,
self.cb.name(), self.name) self.cb.name(), self.name)
log.debug('Spawning thread %s' % threadName) log.debug('Spawning thread %s' % threadName)
threading.Thread.__init__(self, target=target, self.__parent.__init__(target=target, name=threadName,
name=threadName, args=args, kwargs=kwargs) args=args, kwargs=kwargs)
self.setDaemon(True) self.setDaemon(True)
self.originalThreaded = self.cb.threaded self.originalThreaded = self.cb.threaded
self.cb.threaded = True self.cb.threaded = True
def run(self): def run(self):
try: try:
threading.Thread.run(self) self.__parent.run()
finally: finally:
self.cb.threaded = self.originalThreaded self.cb.threaded = self.originalThreaded
@ -1145,14 +1150,18 @@ class SimpleProxy(RichReplyMethods):
def error(self, s, msg=None, **kwargs): def error(self, s, msg=None, **kwargs):
if msg is None: if msg is None:
msg = self.msg msg = self.msg
self.irc.queueMsg(error(msg, s, **kwargs)) m = error(msg, s, **kwargs)
self.irc.queueMsg(m)
return m
def reply(self, s, msg=None, **kwargs): def reply(self, s, msg=None, **kwargs):
if msg is None: if msg is None:
msg = self.msg msg = self.msg
assert not isinstance(s, ircmsgs.IrcMsg), \ assert not isinstance(s, ircmsgs.IrcMsg), \
'Old code alert: there is no longer a "msg" argument to reply.' 'Old code alert: there is no longer a "msg" argument to reply.'
self.irc.queueMsg(reply(msg, s, **kwargs)) m = reply(msg, s, **kwargs)
self.irc.queueMsg(m)
return m
def __getattr__(self, attr): def __getattr__(self, attr):
return getattr(self.irc, attr) return getattr(self.irc, attr)

View File

@ -56,7 +56,17 @@ starting = False
mainThread = threading.currentThread() mainThread = threading.currentThread()
assert 'MainThread' in repr(mainThread) assert 'MainThread' in repr(mainThread)
def isMainThread():
return mainThread is threading.currentThread()
threadsSpawned = 1 # Starts at one for the initial "thread." threadsSpawned = 1 # Starts at one for the initial "thread."
class SupyThread(threading.Thread):
def __init__(self, *args, **kwargs):
global threadsSpawned
threadsSpawned += 1
super(SupyThread, self).__init__(*args, **kwargs)
commandsProcessed = 0 commandsProcessed = 0
ircs = [] # A list of all the IRCs. ircs = [] # A list of all the IRCs.
@ -145,7 +155,10 @@ def makeIrcsDie():
"""Kills Ircs.""" """Kills Ircs."""
log.info('Killing Irc objects.') log.info('Killing Irc objects.')
for irc in ircs[:]: for irc in ircs[:]:
irc.die() if not irc.zombie:
irc.die()
else:
log.debug('Not killing %s, it\'s already a zombie.', irc)
def startDying(): def startDying():
"""Starts dying.""" """Starts dying."""