-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgr.py
352 lines (316 loc) · 8.73 KB
/
msgr.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# Author: Hansheng Zhao <copyrighthero@gmail.com> (https://www.zhs.me)
from multiprocessing import Queue
from sys import version_info
if version_info[0] == 2:
from Queue import Empty
else:
from queue import Empty
# import directive
__all__ = (
'__author__', '__license__', '__version__',
'MessageQueue', 'MessageBroker'
)
# package metadata
__author__ = 'Hansheng Zhao'
__license__ = 'BSD-2-Clause + MIT'
__version__ = '1.0.0'
class MessageQueue(object):
""" MessageQueue class for queues """
__slots__ = ('_queue', )
def __init__(
self, queue = None, messages = None, *args
):
"""
MessageQueue class constructor
:param queue: queue|None, queue instance
:param messages: tuple|list|None, messages
:param args: mixed, default messages
"""
# initialize queue variable
self._queue = Queue() \
if queue is None else queue
# initialize default values
if messages:
for item in messages: self.put(item)
if args:
for item in args: self.put(item)
def __call__(
self, message = None, *args, **kwargs
):
"""
Alias for get & put a message to queue
:param message: mixed|None, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None|mixed, a message
"""
# check if message is set
if message is None:
# acquire a message
return self.get(*args, **kwargs)
else:
# dispatch a message
self.put(message, *args, **kwargs)
def __getattr__(self, key):
"""
Expose queue's attributes
:param key: str, the attribute name
:return: mixed, the queue properties
"""
return self._queue.__getattribute__(key)
def put(self, message, *args, **kwargs):
"""
Put one message to queue with blocking
:param message: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None
"""
# put a message
self._queue.put(message, *args, **kwargs)
# aliases for put
set = send = put
def get(
self,
block = True, timeout = None,
*args, **kwargs
):
"""
Get one message from queue with blocking
:param block: bool, whether block or not
:param timeout, int|None, the timeout
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, a message
"""
# check if blocking and empty
if not block and self._queue.empty():
return None
# acquire a message
try:
message = self._queue.get(
block, timeout, *args, **kwargs
)
except Empty:
return None
return message
# aliases for get
recv = get
# alias for get & put message
msg = property(get, put)
class MessageBroker(object):
""" MessageBroker class for messages """
__slots__ = (
'_job', '_resolve', '_reject', '_service'
)
def __init__(
self,
job = None, res = None,
rej = None, ser = None
):
"""
MessageBroker class constructor
:param job: message_queue|None, queue
:param res: message_queue|None, queue
:param rej: message_queue|None, queue
:param ser: message_queue|None, queue
"""
self._job = MessageQueue() \
if job is None else job
self._resolve = res
self._reject = rej
self._service = ser
def __call__(
self, message = None, *args, **kwargs
):
"""
Alias for get & put a job to queue
:param message: str, a message
:param args: mixed, the arguments
:param kwargs: dict, the kwargs
:return: None|mixed, a message
"""
# check if message is set
if message is None:
# acquire a message
return self.acquire(*args, **kwargs)
else:
# dispatch a message
self.dispatch(message, *args, **kwargs)
def __getattr__(self, key):
"""
Expose job queue's attributes.
:param key: str, the attribute name
:return: mixed, the attributes.
"""
return self._job.__getattribute__(key)
def acquire(
self,
block = True, timeout = None,
*args, **kwargs
):
"""
Acquire a message from job queue
:param block: bool, whether wait or not
:param timeout: numeric, wait time
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, a value
"""
# acquire a message from job queue
return self._job.get(
block = block, timeout = timeout,
*args, **kwargs
)
# aliases for acquire
get = recv = acquire
def dispatch(self, message, *args, **kwargs):
"""
Dispatch a message to job queue
:param message: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None
"""
# dispatch a message to job queue
self._job.put(message, *args, **kwargs)
# aliases for dispatch
set = put = send = dispatch
def settle(
self,
block = False, timeout = 0, *args, **kwargs
):
"""
Settle a succeeded result non-blocking
:param block: bool, whether wait or not
:param timeout: numeric, wait time
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, result object or None
"""
# check if resolve queue is valid
if self._resolve is None: return None
# acquire a resolve message
try:
return self._resolve.get(
block = block, timeout = timeout,
*args, **kwargs
)
except Empty:
return None
def resolve(self, message, *args, **kwargs):
"""
Dispatch a succeeded result
:param message: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None
"""
# dispatch a succeeded result to queue
if self._resolve: self._resolve.put(
message, *args, **kwargs
)
def handle(
self,
block = False, timeout = 0, *args, **kwargs
):
"""
Handle a failed result non-blocking
:param block: bool, whether wait or not
:param timeout: numeric, wait for how long
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, result object or None
"""
# check if resolve queue is valid
if self._reject is None: return None
# acquire a resolve message
try:
return self._reject.get(
block = block, timeout = timeout,
*args, **kwargs
)
except Empty:
return None
def reject(self, message, *args, **kwargs):
"""
Dispatch a failed result
:param message: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None
"""
# dispatch a failed result to queue
if self._reject: self._reject.put(
message, *args, **kwargs
)
def inspect(
self,
block = False, timeout = 0, *args, **kwargs
):
"""
Acquire a pair of results non-blocking
:param block: bool, whether wait or not
:param timeout: numeric, wait for how long
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: tuple(succeeded, failed), results
"""
# acquire a pair of results
return (
self.settle(block, timeout, *args, **kwargs),
self.handle(block, timeout, *args, **kwargs)
)
def conclude(
self, resolve, reject, *args, **kwargs
):
"""
Dispatch a pair of results
:param resolve: mixed, a message
:param reject: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: None
"""
self.resolve(resolve, *args, **kwargs)
self.reject(reject, *args, **kwargs)
def process(
self,
block = True, timeout = None, *args, **kwargs
):
"""
Acquire a message from service queue
:param block: bool, whether wait or not
:param timeout: numeric, wait for how long
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, a value
"""
# acquire a message from service queue
return self._service.get(
block, timeout, *args, **kwargs
) if self._service else None
def request(self, message, *args, **kwargs):
"""
Dispatch a service request
:param message: mixed, a message
:param args: mixed, the arguments
:param kwargs: dict, the kw arguments
:return: mixed, a value
"""
# dispatch a service request
if self._service: self._service.put(
message, *args, **kwargs
)
# alias for get & put a message object
msg = property(acquire, dispatch)
# alias for get & put a message object
job = property(acquire, dispatch)
# alias for get a pair of results
ram = property(inspect)
# alias for get & put a success result
res = property(settle, resolve)
# alias for get & put a failed result
rej = property(handle, reject)
# alias for get & put service request
ser = property(process, request)