From: Eric S. Raymond Date: Sat, 25 Aug 2012 12:32:57 +0000 (-0400) Subject: Thread implementation seems to work. X-Git-Tag: 1.0~138 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=da8385fcb9526068d67d40243bc1d9cda0dbe3b8;p=irker.git Thread implementation seems to work. --- diff --git a/irker.py b/irker.py index d99a5da..396ac30 100755 --- a/irker.py +++ b/irker.py @@ -5,17 +5,35 @@ irker - a simple IRC multiplexer daemon Takes JSON objects of the form {'channel':, 'message':} and relays to IRC channels. +Requires Python 2.6. + """ import os, sys, json, irclib, getopt +import threading, Queue -class Session: +class Session(): "IRC session and message queue processing." def __init__(self, channel): self.channel = channel - self.queue = [] + self.queue = Queue.Queue() + self.thread = threading.Thread(target=self.dequeue) + self.thread.daemon = True + self.thread.start() def enqueue(self, message): "Enque a message for transmission." - self.queue.append(message) + self.queue.put(message) + def dequeue(self): + "Try to ship pending messages from the queue." + while True: + message = self.queue.get() + self.ship(self.channel, message) + self.queue.task_done() + def await(self): + "Block until processing of the queue is done." + self.queue.join() + def ship(self, channel, message): + "Ship a message to the channel." + print "%s: %s" % (channel, message) class Irker: "Persistent IRC multiplexer." @@ -24,7 +42,7 @@ class Irker: def logerr(self, errmsg): "Log a processing error." sys.stderr.write("irker: " + errmsg + "\n") - def run(self, ifp): + def run(self, ifp, await=True): "Accept JSON relay requests from specified stream." while True: inp = ifp.readline() @@ -36,6 +54,9 @@ class Irker: self.logerr("can't recognize JSON on input.") break self.relay(request) + if await: + for session in self.sessions.values(): + session.await() def relay(self, request): if "channel" not in request or "message" not in request: self.logerr("ill-formed reqest")