diff --git a/ipykernel/inprocess/client.py b/ipykernel/inprocess/client.py index ea964ecde..1ed936b20 100644 --- a/ipykernel/inprocess/client.py +++ b/ipykernel/inprocess/client.py @@ -28,6 +28,13 @@ # ----------------------------------------------------------------------------- +def in_running_loop() -> bool: + try: + return asyncio.get_running_loop() is not None + except RuntimeError: + return False + + class InProcessKernelClient(KernelClient): """A client for an in-process kernel. @@ -189,7 +196,7 @@ def _dispatch_to_kernel(self, msg): stream = kernel.shell_stream self.session.send(stream, msg) msg_parts = stream.recv_multipart() - if run_sync is not None: + if not in_running_loop() and run_sync is not None: dispatch_shell = run_sync(kernel.dispatch_shell) dispatch_shell(msg_parts) else: