1
# gozerbot/threads/threadloop.py
5
""" class to implement start/stoppable threads. """
7
__copyright__ = 'this file is in the public domain'
10
from gozerbot.utils.log import rlog
13
from thr import start_new_thread
18
class ThreadLoop(object):
20
""" implement startable/stoppable threads. """
22
def __init__(self, name="", queue=None):
23
self.name = name or 'idle'
27
self.queue = queue or Queue.Queue()
28
self.nowrunning = "none"
31
rlog(0, self.name, 'starting threadloop')
34
while not self.stopped:
37
data = self.queue.get()
49
rlog(-1, self.name, 'running %s' % str(data))
53
rlog(0, self.name, 'stopping threadloop')
57
""" put data on task queue. """
59
self.queue.put_nowait(data)
63
""" start the thread. """
66
start_new_thread(self._loop, ())
70
""" stop the thread. """
76
def handle(self, *args, **kwargs):
78
""" overload this. """
82
class RunnerLoop(ThreadLoop):
84
""" dedicated threadloop for bot commands/callbacks. """
88
rlog(0, self.name, 'starting threadloop')
91
while not self.stopped:
94
data = self.queue.get()
107
self.nowrunning = data[0]
108
rlog(0, self.name, 'now running %s' % self.nowrunning)
112
rlog(0, self.name, 'stopping threadloop')