Show More
@@ -0,0 +1,44 b'' | |||||
|
1 | from kernelmanager import SubSocketChannel | |||
|
2 | from Queue import Queue, Empty | |||
|
3 | ||||
|
4 | ||||
|
5 | class MsgNotReady(Exception): | |||
|
6 | pass | |||
|
7 | ||||
|
8 | ||||
|
9 | class BlockingSubSocketChannel(SubSocketChannel): | |||
|
10 | ||||
|
11 | def __init__(self, context, session, address=None): | |||
|
12 | super(BlockingSubSocketChannel, self).__init__(context, session, address) | |||
|
13 | self._in_queue = Queue() | |||
|
14 | ||||
|
15 | def call_handlers(self, msg): | |||
|
16 | self._in_queue.put(msg) | |||
|
17 | ||||
|
18 | def msg_ready(self): | |||
|
19 | """Is there a message that has been received?""" | |||
|
20 | if self._in_queue.qsize() == 0: | |||
|
21 | return False | |||
|
22 | else: | |||
|
23 | return True | |||
|
24 | ||||
|
25 | def get_msg(self, block=True, timeout=None): | |||
|
26 | """Get a message if there is one that is ready.""" | |||
|
27 | try: | |||
|
28 | msg = self.in_queue.get(block, timeout) | |||
|
29 | except Empty: | |||
|
30 | raise MsgNotReady('No message has been received.') | |||
|
31 | else: | |||
|
32 | return msg | |||
|
33 | ||||
|
34 | def get_msgs(self): | |||
|
35 | """Get all messages that are currently ready.""" | |||
|
36 | msgs = [] | |||
|
37 | while True: | |||
|
38 | try: | |||
|
39 | msg = self.get_msg(block=False) | |||
|
40 | except MsgNotReady: | |||
|
41 | break | |||
|
42 | else: | |||
|
43 | msgs.append(msg) | |||
|
44 | return msgs No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now