Skip to content

Commit 0258ae9

Browse files
author
Quentin Peter
committed
Separate updating and networking code
1 parent 190e8ba commit 0258ae9

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

ipykernel/kernelbase.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from traitlets.config.configurable import SingletonConfigurable
3232
from IPython.core.error import StdinNotImplementedError, UsageError
3333
from ipython_genutils import py3compat
34-
from ipython_genutils.py3compat import unicode_type, string_types
34+
from ipython_genutils.py3compat import unicode_type, string_types, PY3
3535
from ipykernel.jsonutil import json_clean
3636
from traitlets import (
3737
Any, Instance, Float, Dict, List, Set, Integer, Unicode, Bool,
@@ -920,8 +920,8 @@ def _input_request(self, prompt, ident, parent, password=False):
920920
self._stdin_msg = None
921921
# Send the input request.
922922
content = json_clean(dict(prompt=prompt, password=password))
923-
self.session.send(self.stdin_socket, u'input_request', content, parent,
924-
ident=ident)
923+
self.session.send(self.stdin_socket, u'input_request',
924+
content, parent, ident=ident)
925925
# Await a response.
926926
reply = self._wait_input_request_reply()
927927

@@ -943,32 +943,41 @@ def _wait_input_request_reply(self):
943943
KeyboardInterrupt if a keyboard interrupt is recieved.
944944
"""
945945
# Await a response.
946-
reply = None
947-
while reply is None:
946+
while True:
948947
try:
949-
reply = self._input_request_loop_step()
948+
# Try a non blocking recv
949+
ident, reply = self.session.recv(
950+
self.stdin_socket, zmq.NOBLOCK)
951+
if reply:
952+
return reply
953+
if not self._input_request_loop_step():
954+
# Wait until a reply is recieved
955+
ident, reply = self.session.recv(self.stdin_socket, 0)
956+
return reply
950957
except Exception:
951958
self.log.warning("Invalid Message:", exc_info=True)
952959
except KeyboardInterrupt:
953960
# re-raise KeyboardInterrupt, to truncate traceback
954961
raise KeyboardInterrupt
955-
return reply
956962

957963
def _input_request_loop_step(self):
958-
"""Do one step of the input request loop."""
964+
"""
965+
Do one step of the input request loop.
966+
967+
Returns False if no additional steps are needed, otherwise True.
968+
"""
959969
# Allow GUI event loop to update
960-
if sys.version_info >= (3, 4):
961-
is_main_thread = (threading.current_thread() is
962-
threading.main_thread())
970+
if PY3:
971+
is_main_thread = (
972+
threading.current_thread() is threading.main_thread())
963973
else:
964-
is_main_thread = isinstance(threading.current_thread(),
965-
threading._MainThread)
974+
is_main_thread = isinstance(
975+
threading.current_thread(), threading._MainThread)
966976
if is_main_thread and self.eventloop and self._input_eventloop:
967977
self.eventloop(self)
968-
return self._stdin_msg
969-
else:
970-
ident, reply = self.session.recv(self.stdin_socket, 0)
971-
return reply
978+
return True
979+
# No more steps are needed
980+
return False
972981

973982
def _at_shutdown(self):
974983
"""Actions taken at shutdown by the kernel, called by python's atexit.

0 commit comments

Comments
 (0)