首页 > 解决方案 > 在 WSL2 上与 Jupyter Notebook 的连接缓慢

问题描述

我在 WSL2 实例中运行 Jupyter 笔记本时遇到了一个奇怪的问题。每次我启动一个新笔记本时,我第一次连接localhost:8888(或localhost:XXXX任何其他端口号)需要很长时间才能连接,比如 5-10 分钟。但是,一旦与该端口建立了一个连接,所有后续连接都会以正常速度发生。GET据我所知,延迟并没有具体发生在 Jupyter 服务器中,因为服务器输出在等待期间没有显示任何请求:

> jupyter notebook .
[I 08:29:21.282 NotebookApp] Authentication of /metrics is OFF, since other authentication is disabled.
[W 08:29:21.729 NotebookApp] All authentication is disabled.  Anyone who can connect to this server will be able to run code.
[I 08:29:21.732 NotebookApp] Serving notebooks from local directory: /home/peter/jade_poplar/code/python
[I 08:29:21.732 NotebookApp] Jupyter Notebook 6.4.0 is running at:
[I 08:29:21.732 NotebookApp] http://localhost:8888/
[I 08:29:21.732 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
...
5-10 MINUTES ELAPSE
...
[I 08:38:12.718 NotebookApp] 302 GET / (127.0.0.1) 2.110000ms

但是,问题似乎仅限于 Jupyter:如果我只是在端口上侦听,然后将nc -l 8888浏览器指向请求,则会立即出现。localhost:8888GET

我已经尝试随机修复我在 SE 上发现的其他问题,例如。这一个,但无济于事。任何建议,甚至是关于从哪里开始寻找的建议,将不胜感激!这让我发疯了,虽然我在等待笔记本加载时下棋很多:D

标签: jupyter-notebookjupyterwsl-2

解决方案


更新

经过一些调试后,我发现该--no-browser选项也可以解决问题:

jupyter notebook --no-browser .

jupyter-notebook默认localhost:8888在浏览器中打开。我认为 WSL2 在打开默认浏览器时出现问题,因此会超时。


旧答案

我找到了两种方法来解决我系统上的“等待时间”。

  1. 在控制台输出挂起后按Ctrl+C一次Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
  2. DISPLAY环境变量设置为空:DISPLAY= jupyter notebook .. 显然,同样的技巧适用于 pip

推荐阅读