Show More
@@ -1,171 +1,143 b'' | |||||
1 | """Blocking channels |
|
1 | """Blocking channels | |
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) IPython Development Team. |
|
6 | # Copyright (c) IPython Development Team. | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 |
|
8 | |||
9 | try: |
|
9 | try: | |
10 | from queue import Queue, Empty # Py 3 |
|
10 | from queue import Queue, Empty # Py 3 | |
11 | except ImportError: |
|
11 | except ImportError: | |
12 | from Queue import Queue, Empty # Py 2 |
|
12 | from Queue import Queue, Empty # Py 2 | |
13 |
|
13 | |||
14 | from IPython.kernel.channelsabc import ShellChannelABC, IOPubChannelABC, \ |
|
14 | from IPython.kernel.channelsabc import ShellChannelABC, IOPubChannelABC, \ | |
15 | StdInChannelABC |
|
15 | StdInChannelABC | |
16 | from IPython.kernel.channels import HBChannel,\ |
|
16 | from IPython.kernel.channels import HBChannel,\ | |
17 | make_iopub_socket, make_shell_socket, make_stdin_socket,\ |
|
17 | make_iopub_socket, make_shell_socket, make_stdin_socket,\ | |
18 | InvalidPortNumber, major_protocol_version |
|
18 | InvalidPortNumber, major_protocol_version | |
19 | from IPython.utils.py3compat import string_types, iteritems |
|
19 | from IPython.utils.py3compat import string_types, iteritems | |
20 |
|
20 | |||
21 | # some utilities to validate message structure, these might get moved elsewhere |
|
21 | # some utilities to validate message structure, these might get moved elsewhere | |
22 | # if they prove to have more generic utility |
|
22 | # if they prove to have more generic utility | |
23 |
|
23 | |||
24 | def validate_string_list(lst): |
|
24 | def validate_string_list(lst): | |
25 | """Validate that the input is a list of strings. |
|
25 | """Validate that the input is a list of strings. | |
26 |
|
26 | |||
27 | Raises ValueError if not.""" |
|
27 | Raises ValueError if not.""" | |
28 | if not isinstance(lst, list): |
|
28 | if not isinstance(lst, list): | |
29 | raise ValueError('input %r must be a list' % lst) |
|
29 | raise ValueError('input %r must be a list' % lst) | |
30 | for x in lst: |
|
30 | for x in lst: | |
31 | if not isinstance(x, string_types): |
|
31 | if not isinstance(x, string_types): | |
32 | raise ValueError('element %r in list must be a string' % x) |
|
32 | raise ValueError('element %r in list must be a string' % x) | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | def validate_string_dict(dct): |
|
35 | def validate_string_dict(dct): | |
36 | """Validate that the input is a dict with string keys and values. |
|
36 | """Validate that the input is a dict with string keys and values. | |
37 |
|
37 | |||
38 | Raises ValueError if not.""" |
|
38 | Raises ValueError if not.""" | |
39 | for k,v in iteritems(dct): |
|
39 | for k,v in iteritems(dct): | |
40 | if not isinstance(k, string_types): |
|
40 | if not isinstance(k, string_types): | |
41 | raise ValueError('key %r in dict must be a string' % k) |
|
41 | raise ValueError('key %r in dict must be a string' % k) | |
42 | if not isinstance(v, string_types): |
|
42 | if not isinstance(v, string_types): | |
43 | raise ValueError('value %r in dict must be a string' % v) |
|
43 | raise ValueError('value %r in dict must be a string' % v) | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class ZMQSocketChannel(object): |
|
46 | class ZMQSocketChannel(object): | |
47 | """The base class for the channels that use ZMQ sockets.""" |
|
47 | """The base class for the channels that use ZMQ sockets.""" | |
48 | context = None |
|
|||
49 | session = None |
|
48 | session = None | |
50 | socket = None |
|
49 | socket = None | |
51 | ioloop = None |
|
|||
52 | stream = None |
|
50 | stream = None | |
53 | _address = None |
|
|||
54 | _exiting = False |
|
51 | _exiting = False | |
55 | proxy_methods = [] |
|
52 | proxy_methods = [] | |
56 |
|
53 | |||
57 | def __init__(self, socket, session): |
|
54 | def __init__(self, socket, session): | |
58 | """Create a channel. |
|
55 | """Create a channel. | |
59 |
|
56 | |||
60 | Parameters |
|
57 | Parameters | |
61 | ---------- |
|
58 | ---------- | |
62 | context : :class:`zmq.Context` |
|
59 | context : :class:`zmq.Context` | |
63 | The ZMQ context to use. |
|
60 | The ZMQ context to use. | |
64 | session : :class:`session.Session` |
|
61 | session : :class:`session.Session` | |
65 | The session to use. |
|
62 | The session to use. | |
66 | address : zmq url |
|
63 | address : zmq url | |
67 | Standard (ip, port) tuple that the kernel is listening on. |
|
64 | Standard (ip, port) tuple that the kernel is listening on. | |
68 | """ |
|
65 | """ | |
69 | super(ZMQSocketChannel, self).__init__() |
|
66 | super(ZMQSocketChannel, self).__init__() | |
70 | self.daemon = True |
|
|||
71 |
|
67 | |||
72 | self.socket = socket |
|
68 | self.socket = socket | |
73 | self.session = session |
|
69 | self.session = session | |
74 |
|
70 | |||
75 | def _recv(self, **kwargs): |
|
71 | def _recv(self, **kwargs): | |
76 | msg = self.socket.recv_multipart(**kwargs) |
|
72 | msg = self.socket.recv_multipart(**kwargs) | |
77 | ident,smsg = self.session.feed_identities(msg) |
|
73 | ident,smsg = self.session.feed_identities(msg) | |
78 | return self.session.deserialize(smsg) |
|
74 | return self.session.deserialize(smsg) | |
79 |
|
75 | |||
80 | def get_msg(self, block=True, timeout=None): |
|
76 | def get_msg(self, block=True, timeout=None): | |
81 | """ Gets a message if there is one that is ready. """ |
|
77 | """ Gets a message if there is one that is ready. """ | |
82 | if block: |
|
78 | if block: | |
83 | if timeout is not None: |
|
79 | if timeout is not None: | |
84 | timeout *= 1000 # seconds to ms |
|
80 | timeout *= 1000 # seconds to ms | |
85 | ready = self.socket.poll(timeout) |
|
81 | ready = self.socket.poll(timeout) | |
86 | else: |
|
82 | else: | |
87 | ready = self.socket.poll(timeout=0) |
|
83 | ready = self.socket.poll(timeout=0) | |
88 |
|
84 | |||
89 | if ready: |
|
85 | if ready: | |
90 | return self._recv() |
|
86 | return self._recv() | |
91 | else: |
|
87 | else: | |
92 | raise Empty |
|
88 | raise Empty | |
93 |
|
89 | |||
94 | def get_msgs(self): |
|
90 | def get_msgs(self): | |
95 | """ Get all messages that are currently ready. """ |
|
91 | """ Get all messages that are currently ready. """ | |
96 | msgs = [] |
|
92 | msgs = [] | |
97 | while True: |
|
93 | while True: | |
98 | try: |
|
94 | try: | |
99 | msgs.append(self.get_msg(block=False)) |
|
95 | msgs.append(self.get_msg(block=False)) | |
100 | except Empty: |
|
96 | except Empty: | |
101 | break |
|
97 | break | |
102 | return msgs |
|
98 | return msgs | |
103 |
|
99 | |||
104 | def msg_ready(self): |
|
100 | def msg_ready(self): | |
105 | """ Is there a message that has been received? """ |
|
101 | """ Is there a message that has been received? """ | |
106 | return bool(self.socket.poll(timeout=0)) |
|
102 | return bool(self.socket.poll(timeout=0)) | |
107 |
|
103 | |||
108 | def close(self): |
|
104 | def close(self): | |
109 | if self.socket is not None: |
|
105 | if self.socket is not None: | |
110 | try: |
|
106 | try: | |
111 | self.socket.close(linger=0) |
|
107 | self.socket.close(linger=0) | |
112 | except Exception: |
|
108 | except Exception: | |
113 | pass |
|
109 | pass | |
114 | self.socket = None |
|
110 | self.socket = None | |
115 | stop = close |
|
111 | stop = close | |
116 |
|
112 | |||
117 | def is_alive(self): |
|
113 | def is_alive(self): | |
118 | return (self.socket is not None) |
|
114 | return (self.socket is not None) | |
119 |
|
115 | |||
120 | @property |
|
116 | @property | |
121 | def address(self): |
|
117 | def address(self): | |
122 | """Get the channel's address as a zmq url string. |
|
118 | """Get the channel's address as a zmq url string. | |
123 |
|
119 | |||
124 | These URLS have the form: 'tcp://127.0.0.1:5555'. |
|
120 | These URLS have the form: 'tcp://127.0.0.1:5555'. | |
125 | """ |
|
121 | """ | |
126 | return self._address |
|
122 | return self._address | |
127 |
|
123 | |||
128 | def _queue_send(self, msg): |
|
124 | def _queue_send(self, msg): | |
129 | """Pass a message to the ZMQ socket to send |
|
125 | """Pass a message to the ZMQ socket to send | |
130 | """ |
|
126 | """ | |
131 | self.session.send(self.socket, msg) |
|
127 | self.session.send(self.socket, msg) | |
132 |
|
128 | |||
133 | def start(self): |
|
129 | def start(self): | |
134 | pass |
|
130 | pass | |
135 |
|
131 | |||
136 |
|
132 | |||
137 | class BlockingShellChannel(ZMQSocketChannel): |
|
|||
138 | """The shell channel for issuing request/replies to the kernel.""" |
|
|||
139 |
|
||||
140 | def start(self): |
|
|||
141 | self.socket = make_stdin_socket(self.context, self.session.bsession, self.address) |
|
|||
142 |
|
||||
143 |
|
||||
144 | class BlockingIOPubChannel(ZMQSocketChannel): |
|
|||
145 | """The iopub channel which listens for messages that the kernel publishes. |
|
|||
146 |
|
||||
147 | This channel is where all output is published to frontends. |
|
|||
148 | """ |
|
|||
149 | def start(self): |
|
|||
150 | self.socket = make_iopub_socket(self.context, self.session.bsession, self.address) |
|
|||
151 |
|
||||
152 | class BlockingStdInChannel(ZMQSocketChannel): |
|
|||
153 | """The stdin channel to handle raw_input requests that the kernel makes.""" |
|
|||
154 | def start(self): |
|
|||
155 | self.socket = make_stdin_socket(self.context, self.session.bsession, self.address) |
|
|||
156 |
|
||||
157 | ShellChannelABC.register(BlockingShellChannel) |
|
|||
158 | IOPubChannelABC.register(BlockingIOPubChannel) |
|
|||
159 | StdInChannelABC.register(BlockingStdInChannel) |
|
|||
160 |
|
||||
161 |
|
133 | |||
162 | class BlockingHBChannel(HBChannel): |
|
134 | class BlockingHBChannel(HBChannel): | |
163 |
|
135 | |||
164 | # This kernel needs quicker monitoring, shorten to 1 sec. |
|
136 | # This kernel needs quicker monitoring, shorten to 1 sec. | |
165 | # less than 0.5s is unreliable, and will get occasional |
|
137 | # less than 0.5s is unreliable, and will get occasional | |
166 | # false reports of missed beats. |
|
138 | # false reports of missed beats. | |
167 | time_to_dead = 1. |
|
139 | time_to_dead = 1. | |
168 |
|
140 | |||
169 | def call_handlers(self, since_last_heartbeat): |
|
141 | def call_handlers(self, since_last_heartbeat): | |
170 | """ Pause beating on missed heartbeat. """ |
|
142 | """ Pause beating on missed heartbeat. """ | |
171 | pass |
|
143 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now