[Twisted-Python] Twisted Words: throttling server output

Jean-Paul Calderone exarkun at divmod.com
Sat Dec 3 14:40:00 EST 2005

On Sat, 3 Dec 2005 10:38:14 -0800, Seventh Holy Scripture <7hs at muchan.org> wrote:
>I've been writing an IRC bot using Twisted Words, and am having  difficulty 
>keeping it from flooding itself off the server. Currently  my output 
>subroutines look like this:
>     def queueMsg(self, channel, msg):
>         self.chanQueue.append(channel)
>         self.msgQueue.append(msg)
>     def emptyMsgQueue(self):
>         for msg in self.msgQueue:
>             channel = self.chanQueue.pop(0)
>             self.msg(channel, msg)
>             if channel[0] == "#":
>                 self.logger.log("<%s> %s" % (self.nickname, msg))
>             time.sleep(0.5)
>         self.msgQueue = []

The time.sleep() call blocks the entire reactor thread.  Eventually, 
the loop exits, the reactor regains control of program flow, notices 
a huge pile of messages in the IRC connection's output buffer, and 
sends them all as fast as it can.  Woops. :)

Take a look at reactor.callLater() or twisted.internet.task.LoopingCall().
These will let you schedule events to happen at particular times without 
blocking the reactor, and thus allowing each message to be sent at a 
reasonable time.


More information about the Twisted-Python mailing list