File tree 3 files changed +38
-4
lines changed
3 files changed +38
-4
lines changed Original file line number Diff line number Diff line change @@ -2092,7 +2092,34 @@ def _init_asyncio_patch(self):
2092
2092
2093
2093
FIXME: if/when tornado supports the defaults in asyncio,
2094
2094
remove and bump tornado requirement for py38
2095
+
2096
+ With the introduction of the async kernel, the existing sync kernel
2097
+ requires the use of nested loops in order to run code synchronously.
2098
+ This is done in `jupyter_client` using the helper util `run_sync`:
2099
+
2100
+ ref: https://github.com/jupyter/jupyter_client/blob/f453b51eeeff9e905c583b7da3905c0e35cfbdf0/jupyter_client/utils.py#L11
2101
+
2102
+ which creates a new event loop and relies on `nest_asyncio` patching
2103
+ to allow nested loops. This requires that *all* potential tasks are
2104
+ patched before executing. When only some tasks are patched it leads to
2105
+ the following issue:
2106
+
2107
+ ref: https://github.com/jupyter/notebook/issues/6164
2108
+
2109
+ So we must call `nest_asyncio.apply()` method as early as possible. It
2110
+ is preferable to do this in the consuming application rather than the
2111
+ `jupyter_client` as it is a global patch and would impact all consumers
2112
+ rather than just the ones that rely on synchronous kernel behavior.
2095
2113
"""
2114
+ import nest_asyncio
2115
+
2116
+ try :
2117
+ nest_asyncio .apply ()
2118
+ except RuntimeError :
2119
+ # nest_asyncio requires a running loop in order to patch.
2120
+ # In tests the loop may not have been created yet.
2121
+ pass
2122
+
2096
2123
if sys .platform .startswith ("win" ) and sys .version_info >= (3 , 8 ):
2097
2124
import asyncio
2098
2125
try :
Original file line number Diff line number Diff line change @@ -167,10 +167,16 @@ def start_thread():
167
167
token = cls .token ,
168
168
** bind_args
169
169
)
170
- if 'asyncio' in sys .modules :
171
- app ._init_asyncio_patch ()
172
- import asyncio
173
- asyncio .set_event_loop (asyncio .new_event_loop ())
170
+ if "asyncio" in sys .modules :
171
+ app ._init_asyncio_patch ()
172
+ import asyncio
173
+
174
+ asyncio .set_event_loop (asyncio .new_event_loop ())
175
+ # Patch the current loop in order to match production
176
+ # behavior
177
+ import nest_asyncio
178
+
179
+ nest_asyncio .apply ()
174
180
# don't register signal handler during tests
175
181
app .init_signal = lambda : None
176
182
# clear log handlers and propagate to root for nose to capture it
Original file line number Diff line number Diff line change 121
121
'jupyter_client>=5.3.4' ,
122
122
'nbformat' ,
123
123
'nbconvert' ,
124
+ 'nest-asyncio>=1.5' ,
124
125
'ipykernel' , # bless IPython kernel for now
125
126
'Send2Trash>=1.8.0' ,
126
127
'terminado>=0.8.3' ,
You can’t perform that action at this time.
0 commit comments