Python 的 tornado框架一直报错的解决方法,其他框架出错可参考
2
网上找好了好久才知道是系统不同的问题,我这里是windows10
File "D:\python38\lib\site-packages\tornado\tcpserver.py", line 244, in start
self.add_sockets(sockets)
File "D:\python38\lib\site-packages\tornado\tcpserver.py", line 165, in add_sockets
File "D:\python38\lib\site-packages\tornado\netutil.py", line 279, in add_accept_handler
io_loop.add_handler(sock, accept_handler, IOLoop.READ)
andler
self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
File "D:\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下单独处理:
import platfrom
if platform.system()=='Windows':
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
根据系统的不同导入不同的库。
在代码中加入上述代码再运行即可,无报错