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