Show More
@@ -1,114 +1,114 b'' | |||||
1 | """Implement a fully blocking kernel manager. |
|
1 | """Implement a fully blocking kernel manager. | |
2 |
|
2 | |||
3 | Useful for test suites and blocking terminal interfaces. |
|
3 | Useful for test suites and blocking terminal interfaces. | |
4 | """ |
|
4 | """ | |
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (C) 2010 The IPython Development Team |
|
6 | # Copyright (C) 2010 The IPython Development Team | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | # Distributed under the terms of the BSD License. The full license is in | |
9 | # the file COPYING.txt, distributed as part of this software. |
|
9 | # the file COPYING.txt, distributed as part of this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | from __future__ import print_function |
|
15 | from __future__ import print_function | |
16 |
|
16 | |||
17 | # Stdlib |
|
17 | # Stdlib | |
18 | from Queue import Queue, Empty |
|
18 | from Queue import Queue, Empty | |
19 |
|
19 | |||
20 | # Our own |
|
20 | # Our own | |
21 | from IPython.utils import io |
|
21 | from IPython.utils import io | |
22 | from IPython.utils.traitlets import Type |
|
22 | from IPython.utils.traitlets import Type | |
23 |
|
23 | |||
24 | from .kernelmanager import (KernelManager, SubSocketChannel, |
|
24 | from .kernelmanager import (KernelManager, SubSocketChannel, | |
25 | XReqSocketChannel, RepSocketChannel, HBSocketChannel) |
|
25 | XReqSocketChannel, RepSocketChannel, HBSocketChannel) | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Functions and classes |
|
28 | # Functions and classes | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 |
|
30 | |||
31 | class BlockingSubSocketChannel(SubSocketChannel): |
|
31 | class BlockingSubSocketChannel(SubSocketChannel): | |
32 |
|
32 | |||
33 | def __init__(self, context, session, address=None): |
|
33 | def __init__(self, context, session, address=None): | |
34 | super(BlockingSubSocketChannel, self).__init__(context, session, address) |
|
34 | super(BlockingSubSocketChannel, self).__init__(context, session, address) | |
35 | self._in_queue = Queue() |
|
35 | self._in_queue = Queue() | |
36 |
|
36 | |||
37 | def call_handlers(self, msg): |
|
37 | def call_handlers(self, msg): | |
38 | io.rprint('[[Sub]]', msg) # dbg |
|
38 | io.rprint('[[Sub]]', msg) # dbg | |
39 | self._in_queue.put(msg) |
|
39 | self._in_queue.put(msg) | |
40 |
|
40 | |||
41 | def msg_ready(self): |
|
41 | def msg_ready(self): | |
42 | """Is there a message that has been received?""" |
|
42 | """Is there a message that has been received?""" | |
43 | if self._in_queue.qsize() == 0: |
|
43 | if self._in_queue.qsize() == 0: | |
44 | return False |
|
44 | return False | |
45 | else: |
|
45 | else: | |
46 | return True |
|
46 | return True | |
47 |
|
47 | |||
48 | def get_msg(self, block=True, timeout=None): |
|
48 | def get_msg(self, block=True, timeout=None): | |
49 | """Get a message if there is one that is ready.""" |
|
49 | """Get a message if there is one that is ready.""" | |
50 | return self.in_queue.get(block, timeout) |
|
50 | return self._in_queue.get(block, timeout) | |
51 |
|
51 | |||
52 | def get_msgs(self): |
|
52 | def get_msgs(self): | |
53 | """Get all messages that are currently ready.""" |
|
53 | """Get all messages that are currently ready.""" | |
54 | msgs = [] |
|
54 | msgs = [] | |
55 | while True: |
|
55 | while True: | |
56 | try: |
|
56 | try: | |
57 | msgs.append(self.get_msg(block=False)) |
|
57 | msgs.append(self.get_msg(block=False)) | |
58 | except Empty: |
|
58 | except Empty: | |
59 | break |
|
59 | break | |
60 | return msgs |
|
60 | return msgs | |
61 |
|
61 | |||
62 |
|
62 | |||
63 |
|
63 | |||
64 | class BlockingXReqSocketChannel(XReqSocketChannel): |
|
64 | class BlockingXReqSocketChannel(XReqSocketChannel): | |
65 |
|
65 | |||
66 | def __init__(self, context, session, address=None): |
|
66 | def __init__(self, context, session, address=None): | |
67 | super(BlockingXReqSocketChannel, self).__init__(context, session, address) |
|
67 | super(BlockingXReqSocketChannel, self).__init__(context, session, address) | |
68 | self._in_queue = Queue() |
|
68 | self._in_queue = Queue() | |
69 |
|
69 | |||
70 | def call_handlers(self, msg): |
|
70 | def call_handlers(self, msg): | |
71 | io.rprint('[[XReq]]', msg) # dbg |
|
71 | io.rprint('[[XReq]]', msg) # dbg | |
72 |
|
72 | |||
73 | def msg_ready(self): |
|
73 | def msg_ready(self): | |
74 | """Is there a message that has been received?""" |
|
74 | """Is there a message that has been received?""" | |
75 | if self._in_queue.qsize() == 0: |
|
75 | if self._in_queue.qsize() == 0: | |
76 | return False |
|
76 | return False | |
77 | else: |
|
77 | else: | |
78 | return True |
|
78 | return True | |
79 |
|
79 | |||
80 | def get_msg(self, block=True, timeout=None): |
|
80 | def get_msg(self, block=True, timeout=None): | |
81 | """Get a message if there is one that is ready.""" |
|
81 | """Get a message if there is one that is ready.""" | |
82 | return self.in_queue.get(block, timeout) |
|
82 | return self._in_queue.get(block, timeout) | |
83 |
|
83 | |||
84 | def get_msgs(self): |
|
84 | def get_msgs(self): | |
85 | """Get all messages that are currently ready.""" |
|
85 | """Get all messages that are currently ready.""" | |
86 | msgs = [] |
|
86 | msgs = [] | |
87 | while True: |
|
87 | while True: | |
88 | try: |
|
88 | try: | |
89 | msgs.append(self.get_msg(block=False)) |
|
89 | msgs.append(self.get_msg(block=False)) | |
90 | except Empty: |
|
90 | except Empty: | |
91 | break |
|
91 | break | |
92 | return msgs |
|
92 | return msgs | |
93 |
|
93 | |||
94 | class BlockingRepSocketChannel(RepSocketChannel): |
|
94 | class BlockingRepSocketChannel(RepSocketChannel): | |
95 | def call_handlers(self, msg): |
|
95 | def call_handlers(self, msg): | |
96 | io.rprint('[[Rep]]', msg) # dbg |
|
96 | io.rprint('[[Rep]]', msg) # dbg | |
97 |
|
97 | |||
98 |
|
98 | |||
99 | class BlockingHBSocketChannel(HBSocketChannel): |
|
99 | class BlockingHBSocketChannel(HBSocketChannel): | |
100 | # This kernel needs rapid monitoring capabilities |
|
100 | # This kernel needs rapid monitoring capabilities | |
101 | time_to_dead = 0.2 |
|
101 | time_to_dead = 0.2 | |
102 |
|
102 | |||
103 | def call_handlers(self, since_last_heartbeat): |
|
103 | def call_handlers(self, since_last_heartbeat): | |
104 | io.rprint('[[Heart]]', since_last_heartbeat) # dbg |
|
104 | io.rprint('[[Heart]]', since_last_heartbeat) # dbg | |
105 |
|
105 | |||
106 |
|
106 | |||
107 | class BlockingKernelManager(KernelManager): |
|
107 | class BlockingKernelManager(KernelManager): | |
108 |
|
108 | |||
109 | # The classes to use for the various channels. |
|
109 | # The classes to use for the various channels. | |
110 | xreq_channel_class = Type(BlockingXReqSocketChannel) |
|
110 | xreq_channel_class = Type(BlockingXReqSocketChannel) | |
111 | sub_channel_class = Type(BlockingSubSocketChannel) |
|
111 | sub_channel_class = Type(BlockingSubSocketChannel) | |
112 | rep_channel_class = Type(BlockingRepSocketChannel) |
|
112 | rep_channel_class = Type(BlockingRepSocketChannel) | |
113 | hb_channel_class = Type(BlockingHBSocketChannel) |
|
113 | hb_channel_class = Type(BlockingHBSocketChannel) | |
114 |
|
114 |
General Comments 0
You need to be logged in to leave comments.
Login now