Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jupyter Notebook : ERROR:asyncio:Exception in callback

I am using Jupyter Notebook in Windows 10 with Python 3.9.6. I have installed Jupyter Notebook with this command pip install jupyter and run it with this command jupyter notebook. So after starting the notebook, in the terminal I see some errors also but so far I don't seem to see any impact because of it. But still wanted to check, what that error actually indicates and is there a way to fix it?

[I 18:25:02.479 NotebookApp] Serving notebooks from local directory: C:\Users\maryo\Desktop\python\jpnb
[I 18:25:02.479 NotebookApp] Jupyter Notebook 6.4.3 is running at:
[I 18:25:02.479 NotebookApp] http://localhost:8888/?token=dfd2534b828985fcc116e9c400fd31381e9c8f19034e39d3
[I 18:25:02.479 NotebookApp]  or http://127.0.0.1:8888/?token=dfd2534b828985fcc116e9c400fd31381e9c8f19034e39d3
[I 18:25:02.479 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 18:25:02.508 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/maryo/AppData/Roaming/jupyter/runtime/nbserver-17088-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=dfd2534b828985fcc116e9c400fd31381e9c8f19034e39d3
     or http://127.0.0.1:8888/?token=dfd2534b828985fcc116e9c400fd31381e9c8f19034e39d3
c:\users\maryo\appdata\local\programs\python\python39\lib\json\encoder.py:257: UserWarning: date_default is deprecated since jupyter_client 7.0.0. Use jupyter_client.jsonutil.json_default.
  return _iterencode(o, 0)
[W 18:27:49.960 NotebookApp] Notebook Covid Data Analysis.ipynb is not trusted
ERROR:asyncio:Exception in callback <TaskStepMethWrapper object at 0x000002C98ECD81F0>()
handle: <Handle <TaskStepMethWrapper object at 0x000002C98ECD81F0>()>
Traceback (most recent call last):
  File "c:\users\maryo\appdata\local\programs\python\python39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-41' coro=<RequestHandler._execute() running at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\web.py:1660> cb=[_HandlerDelegate.execute.<locals>.<lambda>() at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\web.py:2326]> while another task <Task pending name='Task-40' coro=<MappingKernelManager.start_kernel() running at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\notebook\services\kernels\kernelmanager.py:176> cb=[IOLoop.add_future.<locals>.<lambda>() at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\ioloop.py:688]> is being executed.
ERROR:asyncio:Exception in callback <TaskStepMethWrapper object at 0x000002C98DA94880>()
handle: <Handle <TaskStepMethWrapper object at 0x000002C98DA94880>()>
Traceback (most recent call last):
  File "c:\users\maryo\appdata\local\programs\python\python39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
RuntimeError: Cannot enter into task <Task pending name='Task-30' coro=<HTTP1ServerConnection._server_request_loop() running at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\http1connection.py:840> cb=[IOLoop.add_future.<locals>.<lambda>() at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\ioloop.py:688]> while another task <Task pending name='Task-40' coro=<MappingKernelManager.start_kernel() running at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\notebook\services\kernels\kernelmanager.py:176> cb=[IOLoop.add_future.<locals>.<lambda>() at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\ioloop.py:688]> is being executed.
C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\jupyter_client\ioloop\manager.py:9: RuntimeWarning: coroutine 'RequestHandler._execute' was never awaited
  from .restarter import AsyncIOLoopKernelRestarter
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
ERROR:asyncio:Task was destroyed but it is pending!
task: <Task pending name='Task-41' coro=<RequestHandler._execute() running at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\web.py:1660> cb=[_HandlerDelegate.execute.<locals>.<lambda>() at C:\Users\maryo\Desktop\python\jpnb\lib\site-packages\tornado\web.py:2326]>
like image 332
Maryo David Avatar asked Aug 28 '21 09:08

Maryo David


2 Answers

On the bright side, this issue can be avoided by configuring Notebook to run with its AsyncMappingKernelManager instead - which introduces a full async stack

This method is not a permanent method every time you open Jupyter notebook you need to add these settings

jupyter notebook --NotebookApp.kernel_manager_class=notebook.services.kernels.kernelmanager.AsyncMappingKernelManager

or

This method is a permanent method

Step 1

run

jupyter notebook --generate-config

This will create jupyter_notebook_config.py in your Jupyter folder. The Jupyter folder is in your home directory, ~/.jupyter

Step 2

find these two lines in that folder

## The kernel manager class to use.
#  Default: 'notebook.services.kernels.kernelmanager.MappingKernelManager'

under that line add this line

c.NotebookApp.kernel_manager_class = 'notebook.services.kernels.kernelmanager.AsyncMappingKernelManager'

like this

## The kernel manager class to use.
#  Default: 'notebook.services.kernels.kernelmanager.MappingKernelManager'
#  c.NotebookApp.kernel_manager_class = 'notebook.services.kernels.kernelmanager.MappingKernelManager'
c.NotebookApp.kernel_manager_class = 'notebook.services.kernels.kernelmanager.AsyncMappingKernelManager'

Save and run jupyter notebook as normal

like image 84
Dumidu Pramith Avatar answered Oct 18 '22 08:10

Dumidu Pramith


The suggested fix by Dumidu Parmith answer does not work for me due to

The 'kernel_manager_class' trait of <notebook.notebookapp.NotebookApp object at 0x7f0a210acc88> instance must be a type, but 'notebook.services.kernels.kernelmanager.AsyncMappingKernelManager' could not be imported

as mentioned in the comments to his answer.

As mentioned in the corresponding github issue installing jupyter_client<7 solved the issue for me.

So in case of conda, do

conda install 'jupyter_client<7'

and no configuration change is required. This installed version 6.1.2 in my case.

like image 45
Stefan Avatar answered Oct 18 '22 08:10

Stefan