IPython Unhandled exception in event loop on Windows Platform

在 Windows 上的 Python 3.8 用 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...

其實不影響程式運行,但就是很煩,找了一下解法記錄。

 

Solution

Add the following 2 lines to file <path_to_python>\Lib\asyncio\proactor_events.py in function BaseProactorEventLoop._loop_self_reading after line 768 the following code:

try:
    if f is not None:
        f.result() # may raise
    if self._stopping:
        raise exceptions.CancelledError("Event loop is stopping")
    f = self._proactor.recv(self._ssock, 4096)
except exceptions.CancelledError:
    # _close_self_pipe() has been called, stop waiting for data
%LOCALAPPDATA%\Programs\Python\Python38\Lib\asyncio

 

References