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