22
33Useful for test suites and blocking terminal interfaces.
44"""
5- #-----------------------------------------------------------------------------
6- # Copyright (C) 2013 The IPython Development Team
7- #
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.
10- #-----------------------------------------------------------------------------
115
12- #-----------------------------------------------------------------------------
13- # Imports
14- #-----------------------------------------------------------------------------
6+ # Copyright (c) IPython Development Team.
7+ # Distributed under the terms of the Modified BSD License.
158
169try :
1710 from queue import Queue , Empty # Py 3
1811except ImportError :
1912 from Queue import Queue , Empty # Py 2
2013
21- from IPython .kernel .channels import IOPubChannel , HBChannel , \
22- ShellChannel , StdInChannel
2314
24- #-----------------------------------------------------------------------------
25- # Blocking kernel manager
26- #-----------------------------------------------------------------------------
27-
28-
29- class BlockingChannelMixin (object ):
30-
31- def __init__ (self , * args , ** kwds ):
32- super (BlockingChannelMixin , self ).__init__ (* args , ** kwds )
33- self ._in_queue = Queue ()
34-
35- def call_handlers (self , msg ):
36- self ._in_queue .put (msg )
15+ class ZMQSocketChannel (object ):
16+ """A ZMQ socket in a simple blocking API"""
17+ session = None
18+ socket = None
19+ stream = None
20+ _exiting = False
21+ proxy_methods = []
22+
23+ def __init__ (self , socket , session , loop = None ):
24+ """Create a channel.
25+
26+ Parameters
27+ ----------
28+ socket : :class:`zmq.Socket`
29+ The ZMQ socket to use.
30+ session : :class:`session.Session`
31+ The session to use.
32+ loop
33+ Unused here, for other implementations
34+ """
35+ super (ZMQSocketChannel , self ).__init__ ()
36+
37+ self .socket = socket
38+ self .session = session
39+
40+ def _recv (self , ** kwargs ):
41+ msg = self .socket .recv_multipart (** kwargs )
42+ ident ,smsg = self .session .feed_identities (msg )
43+ return self .session .deserialize (smsg )
3744
3845 def get_msg (self , block = True , timeout = None ):
3946 """ Gets a message if there is one that is ready. """
40- if timeout is None :
41- # Queue.get(timeout=None) has stupid uninteruptible
42- # behavior, so wait for a week instead
43- timeout = 604800
44- return self ._in_queue .get (block , timeout )
47+ if block :
48+ if timeout is not None :
49+ timeout *= 1000 # seconds to ms
50+ ready = self .socket .poll (timeout )
51+ else :
52+ ready = self .socket .poll (timeout = 0 )
53+
54+ if ready :
55+ return self ._recv ()
56+ else :
57+ raise Empty
4558
4659 def get_msgs (self ):
4760 """ Get all messages that are currently ready. """
@@ -55,31 +68,25 @@ def get_msgs(self):
5568
5669 def msg_ready (self ):
5770 """ Is there a message that has been received? """
58- return not self ._in_queue .empty ()
59-
60-
61- class BlockingIOPubChannel (BlockingChannelMixin , IOPubChannel ):
62- pass
63-
64-
65- class BlockingShellChannel (BlockingChannelMixin , ShellChannel ):
66- def call_handlers (self , msg ):
67- if msg ['msg_type' ] == 'kernel_info_reply' :
68- self ._handle_kernel_info_reply (msg )
69- return super (BlockingShellChannel , self ).call_handlers (msg )
70-
71-
72- class BlockingStdInChannel (BlockingChannelMixin , StdInChannel ):
73- pass
71+ return bool (self .socket .poll (timeout = 0 ))
7472
73+ def close (self ):
74+ if self .socket is not None :
75+ try :
76+ self .socket .close (linger = 0 )
77+ except Exception :
78+ pass
79+ self .socket = None
80+ stop = close
7581
76- class BlockingHBChannel (HBChannel ):
82+ def is_alive (self ):
83+ return (self .socket is not None )
7784
78- # This kernel needs quicker monitoring, shorten to 1 sec.
79- # less than 0.5s is unreliable, and will get occasional
80- # false reports of missed beats.
81- time_to_dead = 1.
85+ def send ( self , msg ):
86+ """Pass a message to the ZMQ socket to send
87+ """
88+ self . session . send ( self . socket , msg )
8289
83- def call_handlers (self , since_last_heartbeat ):
84- """ Pause beating on missed heartbeat. """
90+ def start (self ):
8591 pass
92+
0 commit comments