liujiusheng / blog

个人博客,blog
19 stars 0 forks source link

tornado多线程python web框架学习 #219

Open liujiusheng opened 2 years ago

liujiusheng commented 2 years ago

根据输出可以看到,http请求还是先进先执行,前面的执行完了才会再启动后面的执行。

import tornado.ioloop
import tornado.web
import time
import random

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        num = random.randint(1,10)
        print('已启动',num)
        time.sleep(num)
        print('已完成',num)
        self.write("Hello, world")

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

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