Show More
@@ -1,121 +1,129 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, HBSocketChannel, |
|
25 | 25 | ShellSocketChannel, StdInSocketChannel) |
|
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, |
|
35 | 35 | address) |
|
36 | 36 | self._in_queue = Queue() |
|
37 | 37 | |
|
38 | 38 | def call_handlers(self, msg): |
|
39 | 39 | #io.rprint('[[Sub]]', msg) # dbg |
|
40 | 40 | self._in_queue.put(msg) |
|
41 | 41 | |
|
42 | 42 | def msg_ready(self): |
|
43 | 43 | """Is there a message that has been received?""" |
|
44 | 44 | if self._in_queue.qsize() == 0: |
|
45 | 45 | return False |
|
46 | 46 | else: |
|
47 | 47 | return True |
|
48 | 48 | |
|
49 | 49 | def get_msg(self, block=True, timeout=None): |
|
50 | 50 | """Get a message if there is one that is ready.""" |
|
51 | if block and timeout is None: | |
|
52 | # never use timeout=None, because get | |
|
53 | # becomes uninterruptible | |
|
54 | timeout = 1e6 | |
|
51 | 55 | return self._in_queue.get(block, timeout) |
|
52 | 56 | |
|
53 | 57 | def get_msgs(self): |
|
54 | 58 | """Get all messages that are currently ready.""" |
|
55 | 59 | msgs = [] |
|
56 | 60 | while True: |
|
57 | 61 | try: |
|
58 | 62 | msgs.append(self.get_msg(block=False)) |
|
59 | 63 | except Empty: |
|
60 | 64 | break |
|
61 | 65 | return msgs |
|
62 | 66 | |
|
63 | 67 | |
|
64 | 68 | class BlockingShellSocketChannel(ShellSocketChannel): |
|
65 | 69 | |
|
66 | 70 | def __init__(self, context, session, address=None): |
|
67 | 71 | super(BlockingShellSocketChannel, self).__init__(context, session, |
|
68 | 72 | address) |
|
69 | 73 | self._in_queue = Queue() |
|
70 | 74 | |
|
71 | 75 | def call_handlers(self, msg): |
|
72 | 76 | #io.rprint('[[Shell]]', msg) # dbg |
|
73 | 77 | self._in_queue.put(msg) |
|
74 | 78 | |
|
75 | 79 | def msg_ready(self): |
|
76 | 80 | """Is there a message that has been received?""" |
|
77 | 81 | if self._in_queue.qsize() == 0: |
|
78 | 82 | return False |
|
79 | 83 | else: |
|
80 | 84 | return True |
|
81 | 85 | |
|
82 | 86 | def get_msg(self, block=True, timeout=None): |
|
83 | 87 | """Get a message if there is one that is ready.""" |
|
88 | if block and timeout is None: | |
|
89 | # never use timeout=None, because get | |
|
90 | # becomes uninterruptible | |
|
91 | timeout = 1e6 | |
|
84 | 92 | return self._in_queue.get(block, timeout) |
|
85 | 93 | |
|
86 | 94 | def get_msgs(self): |
|
87 | 95 | """Get all messages that are currently ready.""" |
|
88 | 96 | msgs = [] |
|
89 | 97 | while True: |
|
90 | 98 | try: |
|
91 | 99 | msgs.append(self.get_msg(block=False)) |
|
92 | 100 | except Empty: |
|
93 | 101 | break |
|
94 | 102 | return msgs |
|
95 | 103 | |
|
96 | 104 | |
|
97 | 105 | class BlockingStdInSocketChannel(StdInSocketChannel): |
|
98 | 106 | |
|
99 | 107 | def call_handlers(self, msg): |
|
100 | 108 | #io.rprint('[[Rep]]', msg) # dbg |
|
101 | 109 | pass |
|
102 | 110 | |
|
103 | 111 | |
|
104 | 112 | class BlockingHBSocketChannel(HBSocketChannel): |
|
105 | 113 | |
|
106 | 114 | # This kernel needs rapid monitoring capabilities |
|
107 | 115 | time_to_dead = 0.2 |
|
108 | 116 | |
|
109 | 117 | def call_handlers(self, since_last_heartbeat): |
|
110 | 118 | #io.rprint('[[Heart]]', since_last_heartbeat) # dbg |
|
111 | 119 | pass |
|
112 | 120 | |
|
113 | 121 | |
|
114 | 122 | class BlockingKernelManager(KernelManager): |
|
115 | 123 | |
|
116 | 124 | # The classes to use for the various channels. |
|
117 | 125 | shell_channel_class = Type(BlockingShellSocketChannel) |
|
118 | 126 | sub_channel_class = Type(BlockingSubSocketChannel) |
|
119 | 127 | stdin_channel_class = Type(BlockingStdInSocketChannel) |
|
120 | 128 | hb_channel_class = Type(BlockingHBSocketChannel) |
|
121 | 129 |
General Comments 0
You need to be logged in to leave comments.
Login now