23
23
CellExecutionComplete ,
24
24
CellExecutionError
25
25
)
26
- from .util import run_sync , await_or_block
26
+ from .util import run_sync , ensure_async
27
27
28
28
29
29
def timestamp ():
@@ -334,20 +334,20 @@ def start_kernel_manager(self):
334
334
async def _async_cleanup_kernel (self ):
335
335
try :
336
336
# Send a polite shutdown request
337
- await await_or_block (self .kc .shutdown )
337
+ await ensure_async (self .kc .shutdown () )
338
338
try :
339
339
# Queue the manager to kill the process, sometimes the built-in and above
340
340
# shutdowns have not been successful or called yet, so give a direct kill
341
341
# call here and recover gracefully if it's already dead.
342
- await await_or_block (self .km .shutdown_kernel , now = True )
342
+ await ensure_async (self .km .shutdown_kernel ( now = True ) )
343
343
except RuntimeError as e :
344
344
# The error isn't specialized, so we have to check the message
345
345
if 'No kernel is running!' not in str (e ):
346
346
raise
347
347
finally :
348
348
# Remove any state left over even if we failed to stop the kernel
349
- await await_or_block (self .km .cleanup )
350
- await await_or_block (self .kc .stop_channels )
349
+ await ensure_async (self .km .cleanup () )
350
+ await ensure_async (self .kc .stop_channels () )
351
351
self .kc = None
352
352
353
353
_cleanup_kernel = run_sync (_async_cleanup_kernel )
@@ -374,12 +374,12 @@ async def async_start_new_kernel_client(self, **kwargs):
374
374
if self .km .ipykernel and self .ipython_hist_file :
375
375
self .extra_arguments += ['--HistoryManager.hist_file={}' .format (self .ipython_hist_file )]
376
376
377
- await await_or_block (self .km .start_kernel , extra_arguments = self .extra_arguments , ** kwargs )
377
+ await ensure_async (self .km .start_kernel ( extra_arguments = self .extra_arguments , ** kwargs ) )
378
378
379
379
self .kc = self .km .client ()
380
- await await_or_block (self .kc .start_channels )
380
+ await ensure_async (self .kc .start_channels () )
381
381
try :
382
- await await_or_block (self .kc .wait_for_ready , timeout = self .startup_timeout )
382
+ await ensure_async (self .kc .wait_for_ready ( timeout = self .startup_timeout ) )
383
383
except RuntimeError :
384
384
await self ._async_cleanup_kernel ()
385
385
raise
@@ -449,7 +449,7 @@ async def async_execute(self, **kwargs):
449
449
await self .async_execute_cell (
450
450
cell , index , execution_count = self .code_cells_executed + 1
451
451
)
452
- msg_id = await await_or_block (self .kc .kernel_info )
452
+ msg_id = await ensure_async (self .kc .kernel_info () )
453
453
info_msg = await self .async_wait_for_reply (msg_id )
454
454
self .nb .metadata ['language_info' ] = info_msg ['content' ]['language_info' ]
455
455
self .set_widgets_metadata ()
@@ -505,7 +505,7 @@ async def _async_poll_for_reply(self, msg_id, cell, timeout, task_poll_output_ms
505
505
deadline = monotonic () + timeout
506
506
while True :
507
507
try :
508
- msg = await await_or_block (self .kc .shell_channel .get_msg , timeout = timeout )
508
+ msg = await ensure_async (self .kc .shell_channel .get_msg ( timeout = timeout ) )
509
509
if msg ['parent_header' ].get ('msg_id' ) == msg_id :
510
510
if self .record_timing :
511
511
cell ['metadata' ]['execution' ]['shell.execute_reply' ] = timestamp ()
@@ -529,7 +529,7 @@ async def _async_poll_for_reply(self, msg_id, cell, timeout, task_poll_output_ms
529
529
530
530
async def _async_poll_output_msg (self , parent_msg_id , cell , cell_index ):
531
531
while True :
532
- msg = await await_or_block (self .kc .iopub_channel .get_msg , timeout = None )
532
+ msg = await ensure_async (self .kc .iopub_channel .get_msg ( timeout = None ) )
533
533
if msg ['parent_header' ].get ('msg_id' ) == parent_msg_id :
534
534
try :
535
535
# Will raise CellExecutionComplete when completed
@@ -552,14 +552,14 @@ async def _async_handle_timeout(self, timeout, cell=None):
552
552
self .log .error ("Timeout waiting for execute reply (%is)." % timeout )
553
553
if self .interrupt_on_timeout :
554
554
self .log .error ("Interrupting kernel" )
555
- await await_or_block (self .km .interrupt_kernel )
555
+ await ensure_async (self .km .interrupt_kernel () )
556
556
else :
557
557
raise CellTimeoutError .error_from_timeout_and_cell (
558
558
"Cell execution timed out" , timeout , cell
559
559
)
560
560
561
561
async def _async_check_alive (self ):
562
- if not await await_or_block (self .kc .is_alive ):
562
+ if not await ensure_async (self .kc .is_alive () ):
563
563
self .log .error ("Kernel died while waiting for execute reply." )
564
564
raise DeadKernelError ("Kernel died" )
565
565
@@ -569,9 +569,10 @@ async def async_wait_for_reply(self, msg_id, cell=None):
569
569
cummulative_time = 0
570
570
while True :
571
571
try :
572
- msg = await await_or_block (
573
- self .kc .shell_channel .get_msg ,
574
- timeout = self .shell_timeout_interval
572
+ msg = await ensure_async (
573
+ self .kc .shell_channel .get_msg (
574
+ timeout = self .shell_timeout_interval
575
+ )
575
576
)
576
577
except Empty :
577
578
await self ._async_check_alive ()
@@ -652,11 +653,12 @@ async def async_execute_cell(self, cell, cell_index, execution_count=None, store
652
653
cell ['metadata' ]['execution' ] = {}
653
654
654
655
self .log .debug ("Executing cell:\n %s" , cell .source )
655
- parent_msg_id = await await_or_block (
656
- self .kc .execute ,
657
- cell .source ,
658
- store_history = store_history ,
659
- stop_on_error = not self .allow_errors
656
+ parent_msg_id = await ensure_async (
657
+ self .kc .execute (
658
+ cell .source ,
659
+ store_history = store_history ,
660
+ stop_on_error = not self .allow_errors
661
+ )
660
662
)
661
663
# We launched a code cell to execute
662
664
self .code_cells_executed += 1
0 commit comments