1
# gozerbot/eventhandler.py
6
event handler. use to dispatch function in main loop.
10
__copyright__ = 'this file is in the public domain'
16
from utils.exception import handle_exception
17
from utils.log import rlog
18
from utils.locking import lockdec
19
from threads.thr import start_new_thread
31
handlerlock = thread.allocate_lock()
32
locked = lockdec(handlerlock)
39
class Eventhandler(object):
42
events are handled in 11 queues with different priorities:
43
queue0 is tried first queue10 last.
52
self.queues[i] = Queue.Queue()
53
self.sortedlist.append(i)
55
self.sortedlist.sort()
56
self.go = Queue.Queue()
64
start the eventhandler thread.
66
.. literalinclude:: ../../gozerbot/eventhandler.py
67
:pyobject: Eventhandler.start
74
start_new_thread(self.handleloop, ())
80
stop the eventhandler thread.
82
.. literalinclude:: ../../gozerbot/eventhandler.py
83
:pyobject: Eventhandler.stop
91
def put(self, speed, func, *args, **kwargs):
94
put item on the queue.
96
.. literalinclude:: ../../gozerbot/eventhandler.py
97
:pyobject: Eventhandler.put
101
self.queues[10-speed].put_nowait((func, args, kwargs))
107
check queues from available functions to execute.
109
.. literalinclude:: ../../gozerbot/eventhandler.py
110
:pyobject: Eventhandler.getready
116
for i in self.sortedlist:
117
if self.queues[i].qsize():
123
def handle_one(self):
126
do 1 loop over ready queues.
128
.. literalinclude:: ../../gozerbot/eventhandler.py
129
:pyobject: Eventhandler.handle_one
133
ready = self.getready()
136
self.dispatch(self.queues[i])
138
def handleloop(self):
141
thread that polls the queues for items to dispatch.
143
.. literalinclude:: ../../gozerbot/eventhandler.py
144
:pyobject: Eventhandler.handleloop
148
rlog(0, 'eventhandler', 'starting handle thread')
150
while not self.stopped:
154
rlog(0, 'eventhandler', 'stopping %s' % str(self))
156
def dispatch(self, queue):
159
dispatch functions from provided queue.
161
.. literalinclude:: ../../gozerbot/eventhandler.py
162
:pyobject: Eventhandler.dispatch
168
todo = queue.get_nowait()
173
(func, args, kwargs) = todo
174
func(*args, **kwargs)
188
# handler to use in main prog
189
mainhandler = Eventhandler()
b'\\ No newline at end of file'