Solution to tornado error in add_reader raise NotImplementedError under windows

I took the previous code and ran it, and encountered the following error:

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

It is caused by python3.8 asyncio using ProactorEventLoop by default on windows instead of the previous SelectorEventLoop. Jupyter relies on tornado, and tornado needs to use SelectorEventLoop on the window, so this error is generated.

Please see the official documentation: https://www.tornadoweb.org/en/stable/index.html#installation

The solution is to add the following code before tornado starts executing and process it separately under windows:

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

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

Guess you like

Origin blog.csdn.net/lojloj/article/details/106798230