Python3.8下使用tornado报错

Python3.8下使用tornado报错_第1张图片

解决办法:

Tornado官网文档

On Windows, Tornado requires the WindowsSelectorEventLoop. This is the default in Python 3.7 and older, but Python 3.8 defaults to an event loop that is not compatible with Tornado. Applications that use Tornado on Windows with Python 3.8 must call asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) at the beginning of their main file/function.

在Windows上,Tornado需要使用WindowsSelectorEventLoop。这是Python 3.7和更早版本的默认值,但Python 3.8默认为事件循环,与Tornado不兼容。在Python 3.8中使用Tornado的应用程序必须在其主文件/函数的开头调用asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())。

import tornado.ioloop
import tornado.web

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

print(platform.system())
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("hello andorid")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler)
    ])


if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

你可能感兴趣的:(Python学习)