【问题标题】:Jupyter Notebook : ERROR:asyncio:Exception in callbackJupyter Notebook:错误:异步:回调中的异常
【发布时间】:2021-08-28 09:50:29
【问题描述】:

我在带有 Python 3.9.6 的 Windows 10 中使用 Jupyter Notebook。我已经使用此命令pip install jupyter 安装了 Jupyter Notebook,并使用此命令jupyter notebook 运行它。因此,在启动笔记本电脑后,在终端中我也看到了一些错误,但到目前为止,我似乎没有看到任何影响。但仍想检查一下,该错误实际上表明了什么,有没有办法修复它?

[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]>

【问题讨论】:

标签: python-3.x windows jupyter-notebook


【解决方案1】:

从好的方面来说,可以通过将 Notebook 配置为使用其 AsyncMappingKernelManager 运行来避免此问题 - 这引入了一个完整的异步堆栈

此方法不是永久方法,每次打开 Jupyter notebook 都需要添加这些设置

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


此方法是永久方法

第 1 步

运行

jupyter notebook --generate-config

这将在您的 Jupyter 文件夹中创建 jupyter_notebook_config.py。 Jupyter 文件夹位于您的主目录中,~/.jupyter

第 2 步

在那个文件夹中找到这两行

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

在该行下添加该行

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

喜欢这个

## 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'

正常保存并运行 jupyter notebook

【讨论】:

  • 如果您收到The 'kernel_manager_class' trait of &lt;notebook.notebookapp.NotebookApp object at 0x7f0a210acc88&gt; instance must be a type, but 'notebook.services.kernels.kernelmanager.AsyncMappingKernelManager' could not be imported,请确保您已在尝试启动笔记本的环境中安装了 jupyter。
【解决方案2】:

Dumidu Parmith answer 建议的修复方法对我不起作用,因为

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

正如他的回答中提到的那样。

正如相应的github issue 中提到的,安装jupyter_client&lt;7 为我解决了这个问题。

所以在 conda 的情况下,做

conda install 'jupyter_client<7'

并且不需要更改配置。在我的情况下,这个安装版本6.1.2

【讨论】:

    【解决方案3】:

    如果您使用 anaconda,请尝试“pip install jupyter”,而不是“conda install jupyter”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2012-02-03
      • 2016-09-10
      • 2019-12-11
      • 2016-12-13
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      相关资源
      最近更新 更多