-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzmqeventloop.py
More file actions
240 lines (128 loc) · 4.72 KB
/
zmqeventloop.py
File metadata and controls
240 lines (128 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python3
# coding: UTF-8
import urwid
import heapq
import time
import zmq
import os
from itertools import count
from urwid.main_loop import EventLoop
zmq_magic = 99999
################################################################################
class zmqEventLoop(EventLoop):
_alarm_break = count()
#############################################################################
def __init__(self):
self._did_something = True
self._alarms = []
self._poller = zmq.Poller()
self._queue_callbacks = {} # Callback functions
self._queue_callbacki = {} # Index to pass to callback function
self._idle_handle = 0
self._idle_callbacks = {}
#############################################################################
def alarm(self, seconds, callback):
handle = (time.time() + seconds, next(self._alarm_break), callback)
heapq.heappush(self._alarms, handle)
return handle
#############################################################################
def remove_alarm(self, handle):
try:
self._alarms.remove(handle)
heapq.heapify(self._alarms)
return True
except ValueError:
return False
#############################################################################
def watch_queue(self, queue, callback, flags=1, index=zmq_magic):
if queue in self._queue_callbacks:
raise ValueError('already watching %r' % queue)
self._poller.register(queue, flags)
self._queue_callbacks[queue] = callback
self._queue_callbacki[queue] = index
return queue
#############################################################################
def remove_watch_queue(self, handle):
try:
try:
self._poller.unregister(handle)
finally:
self._queue_callbacks.pop(handle, None)
self._queue_callbacki.pop(handle, None)
return True
except KeyError:
return False
#############################################################################
def watch_file(self, fd, callback, flags=1):
if isinstance(fd, int):
fd = os.fdopen(fd)
self._poller.register(fd, flags)
self._queue_callbacks[fd.fileno()] = callback
self._queue_callbacki[fd.fileno()] = zmq_magic
return fd
#############################################################################
def remove_watch_file(self, handle):
try:
try:
self._poller.unregister(handle)
finally:
self._queue_callbacks.pop(handle.fileno(), None)
self._queue_callbacki.pop(handle.fileno(), None)
return True
except KeyError:
return False
#############################################################################
def enter_idle(self, callback):
self._idle_handle += 1
self._idle_callbacks[self._idle_handle] = callback
return self._idle_handle
#############################################################################
def remove_enter_idle(self, handle):
try:
del self._idle_callbacks[handle]
return True
except KeyError:
return False
#############################################################################
def _entering_idle(self):
for callback in list(self._idle_callbacks.values()):
callback()
#############################################################################
def run(self):
try:
while True:
try:
self._loop()
except zmq.error.ZMQError as exc:
if exc.errno != errno.EINTR:
raise
except urwid.ExitMainLoop:
pass
#############################################################################
def _loop(self):
if self._alarms or self._did_something:
if self._alarms:
state = 'alarm'
timeout = max(0, self._alarms[0][0] - time.time())
if self._did_something and (not self._alarms or (self._alarms and timeout > 0)):
state = 'idle'
timeout = 0
ready = dict(self._poller.poll(timeout * 1000))
else:
state = 'wait'
ready = dict(self._poller.poll())
if not ready:
if state == 'idle':
self._entering_idle()
self._did_something = False
elif state == 'alarm':
due, tie_break, callback = heapq.heappop(self._alarms)
callback()
self._did_something = True
for queue, _ in ready.items():
if self._queue_callbacki[queue] == zmq_magic: # Default value used for back compatibility
self._queue_callbacks[queue]() # Call for urwid file descriptor
else:
self._queue_callbacks[queue](self._queue_callbacki[queue]) # Call for zmq queue
self._did_something = True
################################################################################