[Python] VSCode Jupyter Notebook - Stop Cell Execution

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I've an issue with proper termination of an async python code running in a jupyter notebook cell. I would like to use the stop button on a jupyter notebook cell to exit cleanly the code running in the cell. I thought at first I could use the asyncio.CancelledError exception as it seems jupyter cell run as async, but I've a weird behavior with the code below :

    import asyncio

    try:
    await asyncio.sleep(3)
    except asyncio.CancelledError as e:
    print("Caught exception")
    print(e)
    except Exception as e:
    print(e)


    In this example if I press the stop button on the cell before the sleep is done, the asyncio.CancelledError is caught but only after the sleep is done...

    Is this expected behavior ? Is there a fix or another signal I could listen to in order to detect when the user request to stop the cell ?

    Continue reading...

Compartilhe esta Página