Ipython: '事件循环中未处理的异常' (WinError 995)

创建于 2019-12-16  ·  43评论  ·  资料来源: ipython/ipython

我最近开始使用 ipython。 现在,我经常在执行任何操作时收到此消息,即使是简单的操作,例如定义变量:

Unhandled exception in event loop:
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\proactor_events.py", line 768, in _loop_self_reading
    f.result()  # may raise
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "c:\users\USER\appdata\local\programs\python\python38-32\lib\asyncio\windows_events.py", line 457, in finish_recv
    raise ConnectionResetError(*exc.args)

Exception [WinError 995] The I/O operation has been aborted because of either a thread exit or an application request
Press ENTER to continue...

对于同一命令(如 xyz = "hello"),有时会显示此消息,有时不会显示此消息。 不知道我做错了什么。 普通的解释器没有这个问题。 使用 3.8 并通过 cmd 安装 ipython 7.10.2。

谢谢。

最有用的评论

经过一番调查,我认为这是 Python 的 asyncio 的 Windows 平台实现中的一个错误:

Windows 10 版本 1809(OS Build 17763.1039)上的 Python 3.8.1(tags/v3.8.1:1b293b6,2019 年 12 月 18 日,23:11:46)[MSC v.1916 64 位(AMD64)]

1) 添加到文件libasyncioproactor_events.py 在函数BaseProactorEventLoop._loop_self_reading后第 768 行以下代码:

766....try:
767........if f is not None:
768............f.result() # may raise
...........if self._stopping:
...............raise exceptions.CancelledError("Event loop is stopping")
769........f = self._proactor.recv(self._ssock, 4096)

2) 测试。 我没有更多错误...

编辑:您可以在这个特定的代码上下文中使用if self._stopping: return代替(应该快一点)

EDIT2:bugs.python.org 上的错误和导致此错误的原因的描述(由其他人发现): https ://bugs.python.org/issue39010#msg362076

所有43条评论

您可以尝试将提示工具包降级到 2.x 以查看是否是这个原因?

我确实降级以提示工具包 2.0.10 并且它有效。 太感谢了。
您能否说,由于我对此完全陌生,发生了什么变化?
我会因为反转版本而遇到问题吗?

Prompt_toolkit 3(允许 IPython 进行多行编辑和语法着色的库)在 IPython 7.10 之前发布; 如果您进行一些并发编程,它会尝试更好地使用 asyncio eventloop。 我们已尽最大努力使 IPython 与提示工具包 3 兼容,但有一些我们可能没有的极端情况。

使用 prompt_toolkit 2.x 应该一切正常,我们将尝试找出您收到上述错误的原因。

由于您当前的错误,很难知道这到底是从哪里来的,但希望在接下来的几个版本中,我们会缩小原因。

知道什么时候会解决这个问题吗?

当有人花时间调查为什么会发生这种情况以及解决方法是什么时; 可能有 Windows 机器的人作为 VM 特别慢。

不幸的是,所有的常规维护者(主要是我)都在空闲时间做这件事——这很少见。 我也没有windows机器。

如果您发现任何线索,总是会触发此操作的一系列动作会有所帮助。

经过一番调查,我认为这是 Python 的 asyncio 的 Windows 平台实现中的一个错误:

Windows 10 版本 1809(OS Build 17763.1039)上的 Python 3.8.1(tags/v3.8.1:1b293b6,2019 年 12 月 18 日,23:11:46)[MSC v.1916 64 位(AMD64)]

1) 添加到文件libasyncioproactor_events.py 在函数BaseProactorEventLoop._loop_self_reading后第 768 行以下代码:

766....try:
767........if f is not None:
768............f.result() # may raise
...........if self._stopping:
...............raise exceptions.CancelledError("Event loop is stopping")
769........f = self._proactor.recv(self._ssock, 4096)

2) 测试。 我没有更多错误...

编辑:您可以在这个特定的代码上下文中使用if self._stopping: return代替(应该快一点)

EDIT2:bugs.python.org 上的错误和导致此错误的原因的描述(由其他人发现): https ://bugs.python.org/issue39010#msg362076

MrEightFive 感谢您的修复.. 它似乎对我有用

@MrEightFive谢谢,刚刚实施了更改。 它似乎工作得很好。

@MrEightFive看起来它也适用于 Python 3.8.2。

感谢@MrEightFive@Carreau的解决方案,非常感谢,我遇到了同样的问题。

@MrEightFive看起来它也适用于 Python 3.8.2。

同样积极的经历

一样,谢谢!

也遇到这种情况。 @MrEightFive如果您认为您知道修复是什么,也许可以向 CPython 提交拉取请求? :眨眼:

@segevfiner
我不是 CPython 开发组的成员,根据他们的文档,在他们让你进入之前有一个(广泛的)验证过程。我不值得为单个拉取请求完成,尽管我会做到的如果它更容易。

其次,bugs.python.org 上已经存在这个错误报告(参见我上面帖子中的 EDIT2),标题不同,但这个错误的原因与我发现的完全一样。 所以我想没有必要复制。

第三,虽然我的解决方法有效,但我不能保证它没有副作用(它与在特定版本的 asyncio 中如何实现事件循环的关闭有一些依赖关系,并且可能会改变)。 我运行了 CPython 的 asyncio 测试(在 3.8.1 上),它们已经通过了,但除此之外我从未进行过调查。 如果有人真正在 asyncio 上工作并且精通 CPython 代码,那就更好了。

@MrEightFive实际上比您想象的要简单。 只需提交 PR 并签署 CLA,然后等待代码审查等。就像 GitHub 中的任何其他项目一样。 尽管 CPython 似乎经常没有足够的人来审查拉取请求,但仍有很多人被搁置。 或者,您可以在 Python 错误跟踪器问题上发布您建议的解决方法。 即使你不确定,你也可以在 PR/issue 中这样说,提出你的担忧。 这通常有助于让更熟悉的人查看问题并验证修复或提供更好/正确的修复。 但这只是一个建议,只有当你真的喜欢它时才去做。

使用 promt_toolkit 时也会出现此错误,但解决方案:MrEightyFive 效果很好,节省了很多时间,谢谢

将代码添加到 proactor_events.py (正如 MrEightFive 建议的那样)并没有帮助,但pip install --upgrade prompt-toolkit==2.0.10做了。 Win10 Python38。

KyryloKaralyus 的解决方案对我有用。 需要注意的一点 - 我第一次不小心运行(最后缺少 0) pip install --upgrade prompt-toolkit==2.0.1 ,这导致错误开始不停地发生。 我不得不杀死终端。

将代码添加到 proactor_events.py (正如 MrEightFive 建议的那样)并没有帮助,但pip install --upgrade prompt-toolkit==2.0.10做了。 Win10 Python38。

这对我有用。

最终为 CPython 提出了拉取请求: bpo-39010:在循环结束后取消未来时忽略错误。

顺便说一句,如果 python 是从 MS Store 安装的,那么修改proactor_events.py是非常困难的。

图片

我将所有者更改为我,并将我和“每个人”设置为完全控制。 但我仍然无法编辑文件,即使在安全模式和“以管理员身份重试”。

我确实降级以提示工具包 2.0.10 并且它有效。 太感谢了。

pip install prompt-toolkit==2.0.10

我在 python 3.8.3、ipython 7.13.0、prompt_toolkit 3.0.5 中遇到了这个异常

顺便说一句,如果 python 是从 MS Store 安装的,那么修改proactor_events.py是非常困难的。

更简单的方法是将整个 asyncio 包复制到您的项目中并在那里进行修改,然后依靠 Python 来隐藏整个模块。 Windows 应用商店应用程序受到_非常_ 良好的保护。

但我也在跟进 CPython 补丁。

经过一番调查,我认为这是 Python 的 asyncio 的 Windows 平台实现中的一个错误:

Windows 10 版本 1809(OS Build 17763.1039)上的 Python 3.8.1(tags/v3.8.1:1b293b6,2019 年 12 月 18 日,23:11:46)[MSC v.1916 64 位(AMD64)]

  1. 添加到文件libasyncioproactor_events.py 在函数BaseProactorEventLoop._loop_self_reading后第 768 行以下代码:

766....try:
767........if f is not None:
768............f.result() # may raise
...........if self._stopping:
...............raise exceptions.CancelledError("Event loop is stopping")
769........f = self._proactor.recv(self._ssock, 4096)

  1. 测试。 我没有更多错误...

编辑:您可以在这个特定的代码上下文中使用if self._stopping: return代替(应该快一点)

EDIT2:bugs.python.org 上的错误和导致此错误的原因的描述(由其他人发现): https ://bugs.python.org/issue39010#msg362076

也为我工作!
谢谢!!

我确实降级以提示工具包 2.0.10 并且它有效。 太感谢了。

pip install prompt-toolkit==2.0.10

非常感谢,这对我有用:

conda install prompt_toolkit=2.0.10

您可以尝试将提示工具包降级到 2.x 以查看是否是此原因

这对我也有帮助,谢谢!

pip install --upgrade prompt-toolkit==2.0.10

非常感谢,我终于可以在我的设备上使用上述解决方案了!!

我尝试执行以下操作,但我总是会得到 [Errno 13] Permission denied

  1. 添加到文件libasyncioproactor_events.py 在函数BaseProactorEventLoop._loop_self_reading后第 768 行以下代码:

这已在 3.9rc2 中修复

@imba-tjd 它会被移植到旧的 Python 版本吗? (或者已经有了?)

从 python 错误来看,t 看起来已经在 3.8 上被反向移植。

即使使用 3.9rc2,我也会收到错误消息...

您可以尝试将提示工具包降级到 2.x 以查看是否是这个原因?

这有帮助,谢谢。

我想知道这现在是否已在 Python 3.8.6 和 3.9 中修复。

经过一番调查,我认为这是 Python 的 asyncio 的 Windows 平台实现中的一个错误:

Windows 10 版本 1809(OS Build 17763.1039)上的 Python 3.8.1(tags/v3.8.1:1b293b6,2019 年 12 月 18 日,23:11:46)[MSC v.1916 64 位(AMD64)]

  1. 添加到文件libasyncioproactor_events.py 在函数BaseProactorEventLoop._loop_self_reading后第 768 行以下代码:

766....try:
767........if f is not None:
768............f.result() # may raise
...........if self._stopping:
...............raise exceptions.CancelledError("Event loop is stopping")
769........f = self._proactor.recv(self._ssock, 4096)

  1. 测试。 我没有更多错误...

编辑:您可以在这个特定的代码上下文中使用if self._stopping: return代替(应该快一点)

EDIT2:bugs.python.org 上的错误和导致此错误的原因的描述(由其他人发现): https ://bugs.python.org/issue39010#msg362076

我在 Windows 10 上使用 Anaconda 全新安装 Python 3.8.3 时遇到了这个问题, @MrEightFive的解决方法对我来说非常好!

pip install --upgrade prompt-toolkit==2.0.10
这在 Windows Anaconda3-2020.11 版本上对我有用。

这(几乎)在 Windows 10, Anaconda Individual Edition 2020.11 上对我有用。

如果您是 Anaconda 用户,您可能希望将此更新保留在 conda 环境中:
conda install -c conda-forge prompt_toolkit

这对我有用。

在我执行 try-except-block 后立即出现,在其他情况下它是不可预测的

只想添加德语错误文本,以便更多用户找到。 (Windows 10 专业版,2004 版)

事件循环中未处理的异常:
_loop_self_reading 中的文件“c:usersuserappdatalocalprogramspythonpython38libasyncioproactor_events.py”,第 768 行
f.result() # 可能会引发
_poll 中的文件“c:usersuserappdatalocalprogramspythonpython38libasynciowindows_events.py”,第 808 行
值 = 回调(转移,键,ov)
文件“c:usersuserappdatalocalprogramspythonpython38libasynciowindows_events.py”,第 457 行,在 finish_recv
引发 ConnectionResetError(*exc.args)

异常 [WinError 995] Der E/A-Vorgang wurde wegen eines Threadendes oder einer Anwendungsanforderung abgebrochen
按 ENTER 继续...

可悲的是

conda install -c conda-forge prompt_toolkit

没有为我解决问题。

@musm ,您需要指定 prompt_toolkit v2。

conda install -c conda-forge prompt_toolkit=2

我在 Anaconda 2020.11 上做

conda install -c conda-forge prompt_toolkit=2

正在导致大规模冲突:

> conda install -c conda-forge prompt_toolkit=2
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: \
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
Examining qtpy:   1%|▌                                                                 | 3/327 [00:00<00:20, 16.00it/s]\Examining statsmodels:   1%|▌                                                          | 3/327 [00:00<00:20, 16.00it/s]/Examining ipython_genutils:   3%|█▌                                                   | 10/327 [00:07<04:17,  1.23it/s]|Examining pyflakes:   5%|██▉                                                          | 16/327 [00:10<02:19,  2.22it/s]|Examining zope.event:   7%|████▏                                                      | 23/327 [00:13<02:18,  2.20it/s]\Examining vs2015_runtime:  11%|██████                                                 | 36/327 [00:17<02:29,  1.94it/s]\Examining mpir:  15%|█████████▌                                                       | 48/327 [01:32<37:15,  8.01s/it]\Examining krb5:  17%|███████████▎                                                     | 57/327 [01:38<06:35,  1.47s/it]-Examining prometheus_client:  18%|█████████▌                                          | 60/327 [01:42<09:06,  2.05s/it]|Examining cython:  19%|████████████▏                                                  | 63/327 [01:47<08:58,  2.04s/it]-Examining flake8:  21%|█████████████                                                  | 68/327 [02:37<24:45,  5.73s/it]-Examining pytest:  25%|███████████████▊                                               | 82/327 [03:30<30:07,  7.38s/it]-Examining openpyxl:  26%|███████████████▊                                             | 85/327 [03:51<25:16,  6.27s/it]\Examining beautifulsoup4:  32%|█████████████████▎                                    | 105/327 [04:08<03:40,  1.01it/s]|Examining html5lib:  35%|████████████████████▉                                       | 114/327 [04:11<01:34,  2.25it/s]/Examining console_shortcut:  35%|██████████████████▏                                 | 114/327 [04:11<01:34,  2.25it/s]-Examining qt:  37%|████████████████████████▌                                         | 122/327 [04:14<01:09,  2.94it/s]/Examining wheel:  38%|████████████████████████                                       | 125/327 [04:14<00:54,  3.71it/s]|Examining greenlet:  39%|███████████████████████▍                                    | 128/327 [04:18<03:24,  1.03s/it]\Examining bokeh:  40%|█████████████████████████                                      | 130/327 [04:19<02:27,  1.33it/s]|Examining bleach:  40%|████████████████████████▊                                     | 131/327 [04:37<19:29,  5.97s/it]|Examining entrypoints:  40%|███████████████████████                                  | 132/327 [04:39<15:25,  4.75s/it]|Examining et_xmlfile:  40%|███████████████████████▍                                  | 132/327 [04:39<15:25,  4.75s/it]/Examining rtree:  42%|██████████████████████████▏                                    | 136/327 [04:39<07:31,  2.37s/it]-Examining backcall:  42%|████████████████████████▉                                   | 136/327 [04:39<07:31,  2.37s/it]\Examining libssh2:  45%|███████████████████████████▌                                 | 148/327 [04:58<06:13,  2.09s/it]\Examining path:  52%|█████████████████████████████████▍                              | 171/327 [05:54<23:54,  9.19s/it]/Examining pywavelets:  53%|██████████████████████████████▌                           | 172/327 [05:55<16:51,  6.53s/it]-Examining importlib_metadata:  54%|███████████████████████████▏                      | 178/327 [06:06<07:34,  3.05s/it]-Examining prompt_toolkit=2:  55%|████████████████████████████▍                       | 179/327 [06:07<07:56,  3.22s/it]-Examining seaborn:  56%|██████████████████████████████████▏                          | 183/327 [06:17<06:30,  2.71s/it]\Examining jupyterlab_widgets:  57%|████████████████████████████▎                     | 185/327 [06:21<05:57,  2.52s/it]|Examining jdcal:  58%|████████████████████████████████████▍                          | 189/327 [06:21<02:55,  1.27s/it]-Examining widgetsnbextension:  59%|█████████████████████████████▌                    | 193/327 [06:22<01:18,  1.70it/s]-Examining multipledispatch:  64%|█████████████████████████████████▍                  | 210/327 [07:07<03:36,  1.85s/it]-Examining yaml:  68%|███████████████████████████████████████████▍                    | 222/327 [07:19<01:11,  1.46it/s]-Examining libxslt:  70%|██████████████████████████████████████████▌                  | 228/327 [07:31<03:34,  2.17s/it]\Examining python-jsonrpc-server:  70%|████████████████████████████████▉              | 229/327 [07:31<02:30,  1.54s/it]|Examining typed-ast:  71%|█████████████████████████████████████████▊                 | 232/327 [07:32<01:01,  1.54it/s]\Examining @/win-64::__archspec==1=x86_64:  71%|██████████████████████████▉           | 232/327 [07:32<01:01,  1.54it/s]|Examining urllib3:  73%|████████████████████████████████████████████▍                | 238/327 [07:42<03:30,  2.36s/it]/Examining backports:  73%|███████████████████████████████████████████                | 239/327 [07:47<04:20,  2.96s/it]\Examining mccabe:  76%|███████████████████████████████████████████████▏          

@musm ,这是使用完整 Anaconda 发行版的缺点之一。 有这么多的模块,如果您偏离提供的发行版,可能很难同时解决它们的所有依赖关系。 您可以尝试使用 Miniconda,它是 Conda,但没有预装模块。 使用它,您可以创建环境以仅包含项目所需的模块。

我还没有尝试过,但您也可以这样做,而无需卸载 Anaconda 并安装 Miniconda。 IE 从头开始​​创建仅包含特定模块的新环境。 尝试使用如下命令(编辑模块名称以包含您真正需要的内容)。

conda create -n <myenv> python ipython prompt_toolkit=2 jupyter pandas numpy scipy scikit-learn matplotlib
此页面是否有帮助?
0 / 5 - 0 等级