windows下tornado报错 in add_reader raise NotImplementedError的解决

之前的代码拿过来跑,遇到报错如下:

Traceback (most recent call last):  
 File ".\index.py", line 325, in <module>     
 app.listen(8888)   
 File "C:\Program Files\Python38\lib\site-packages\tornado\web.py", line 2112, in listen     
 server.listen(port, address)   
 File "C:\Program Files\Python38\lib\site-packages\tornado\tcpserver.py", line 152, in listen     self.add_sockets(sockets)   
 File "C:\Program Files\Python38\lib\site-packages\tornado\tcpserver.py", line 165, in add_sockets     self._handlers[sock.fileno()] = add_accept_handler(   
 File "C:\Program Files\Python38\lib\site-packages\tornado\netutil.py", line 279, in add_accept_handler     io_loop.add_handler(sock, accept_handler, IOLoop.READ)  
 File "C:\Program Files\Python38\lib\site-packages\tornado\platform\asyncio.py", line 99, in add_handler     self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)   
 File "C:\Program Files\Python38\lib\asyncio\events.py", line 501, in add_reader     
 raise NotImplementedError 
 NotImplementedError

是由于 python3.8 asyncio 在 windows 上默认使用 ProactorEventLoop 造成的,而不是之前的 SelectorEventLoop。jupyter 依赖 tornado,而 tornado 在 window 上需要使用 SelectorEventLoop,所以产生这个报错.

请看官方文档:https://www.tornadoweb.org/en/stable/index.html#installation

解决方法是,在 tornado开始执行前添加以下代码,在windows下单独处理:

# windows 系统下 tornado 使用 使用 SelectorEventLoop
import platform

if platform.system() == "Windows":
    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

你可能感兴趣的:(python)