tornado异步请求非阻塞

前言也许有同窗很迷惑:tornado不是标榜异步非阻塞解决10K问题的嘛?可是我却发现不是torando很差,而是你用错了 好比最近发现一个事情:某网

前言前端

也许有同窗很迷惑:tornado不是标榜异步非阻塞解决10K问题的嘛?可是我却发现不是torando很差,而是你用错了.好比最近发现一个事情:某网站打开页面很慢,服务器cpu/内存都正常.网络状态也良好. 后来发现,打开页面会有不少请求后端数据库的访问,有一个mongodb的数据库业务api的rest服务.可是它的tornado却用错了,一步步的来研究问题:python

 

说明git

如下的例子都有2个url,一个是耗时的请求,一个是能够或者说须要马上返回的请求,我想就算一个对技术不熟,从道理上来讲的用户, 他但愿的是他访问的请求不会影响也不会被其余人的请求影响github

 

#!/bin/env pythonweb

import tornado.httpservermongodb

import tornado.ioloop数据库

import tornado.options后端

import tornado.webapi

import tornado.httpclient服务器

import time

from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

class SleepHandler(tornado.web.RequestHandler):

    def get(self):

        time.sleep(5)

        self.write("when i sleep 5s")

class JustNowHandler(tornado.web.RequestHandler):

    def get(self):

        self.write("i hope just now see you")

if __name__ == "__main__":

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[

            (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)])

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()

假如你使用页面请求或者使用哪一个httpie,curl等工具先访问http://localhost:8000/sleep,再访问http://localhost:8000/justnow.你会发现原本能够马上返回的/jsutnow的请求会一直阻塞到/sleep请求完才返回.

 

这是为啥?为啥个人请求被/sleep请求阻塞了?若是平时咱们的web请求足够快咱们可能不会意识到这个问题,可是事实上常常会有一些耗时的进程,意味着应用程序被有效的锁定直至处理结束.

 

这是时候你有没有想起@tornado.web.asynchronous这个装饰器?可是使用这个装饰器有个前提就是你要耗时的执行须要执行异步,好比上面的time.sleep,你只是加装饰器是没有做用的,并且须要注意的是 Tornado默认在函数处理返回时关闭客户端的链接,可是当你使用@tornado.web.asynchonous装饰器时,Tornado永远不会本身关闭链接,须要显式的self.finish()关闭

 

咱们大部分的函数都是阻塞的, 好比上面的time.sleep其实tornado有个异步的实现:

 

#!/bin/env python

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

import tornado.gen

import tornado.httpclient

import tornado.concurrent

import tornado.ioloop

import time

from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

class SleepHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous

    @tornado.gen.coroutine

    def get(self):

        yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 5)

        self.write("when i sleep 5s")

class JustNowHandler(tornado.web.RequestHandler):

    def get(self):

        self.write("i hope just now see you")

if __name__ == "__main__":

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[

            (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)])

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()

这里有个新的tornado.gen.coroutine装饰器, coroutine是3.0以后新增的装饰器.之前的办法是用回调,仍是看我这个例子:

 

class SleepHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous

    def get(self):

        tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 5, callback=self.on_response)

    def on_response(self):

        self.write("when i sleep 5s")

        self.finish()

使用了callback, 可是新的装饰器让咱们经过yield实现一样的效果:你在打开/sleep以后再点击/justnow, justnow的请求都是马上返回不受影响.可是用了asynchronous的装饰器你的耗时的函数也须要执行异步

 

刚才说的都是没有意义的例子,下面写个有点用的:读取mongodb数据库数据,而后再前端按行write出来

 

#!/bin/env python

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

import tornado.gen

import tornado.httpclient

import tornado.concurrent

import tornado.ioloop

import time

# 一个mongodb出品的支持异步的数据库的python驱动

import motor

from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

# db其实就是test数据库的游标

db = motor.MotorClient().open_sync().test

class SleepHandler(BaseHandler):

    @tornado.web.asynchronous

    @tornado.gen.coroutine

    def get(self):

        # 这一行执行仍是阻塞须要时间的,个人tt集合有一些数据而且没有索引

        cursor = db.tt.find().sort([('a', -1)])

        # 这部分会异步非阻塞的执行二不影响其余页面请求

        while (yield cursor.fetch_next):

            message = cursor.next_object()

            self.write('<li>%s</li>' % message['a'])

        self.write('</ul>')

        self.finish()

    def _on_response(self, message, error):

        if error:

            raise tornado.web.HTTPError(500, error)

        elif message:

            for i in message:

                self.write('<li>%s</li>' % i['a'])

        else:

            self.write('</ul>')

            self.finish()

class JustNowHandler(BaseHandler):

    def get(self):

        self.write("i hope just now see you")

if __name__ == "__main__":

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[

            (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)])

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()

一个同事提示为何这个耗时的东西不能异步的丢给某工具去执行而不阻塞个人请求呢?好吧,我也想到了:celery,正好github有这个东西:tornado-celery

 

执行下面的程序首先你要安装rabbitmq和celery:

 

#!/bin/env python

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

import tornado.gen

import tornado.httpclient

import tcelery, tasks

import time

from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

tcelery.setup_nonblocking_producer()

class SleepHandler(tornado.web.RequestHandler):

    @tornado.web.asynchronous

    @tornado.gen.coroutine

    def get(self):

        # tornado.gen.Task的参数是:要执行的函数, 参数

        yield tornado.gen.Task(tasks.sleep.apply_async, args=[5])

        self.write("when i sleep 5s")

        self.finish()

class JustNowHandler(tornado.web.RequestHandler):

    def get(self):

        self.write("i hope just now see you")

if __name__ == "__main__":

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[

            (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)])

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()

task是celery的任务定义的文件,包含咱们说的time.sleep的函数

 

import time

from celery import Celery

celery = Celery("tasks", broker="amqp://guest:guest@localhost:5672")

celery.conf.CELERY_RESULT_BACKEND = "amqp"

@celery.task

def sleep(seconds):

    time.sleep(float(seconds))

    return seconds

if __name__ == "__main__":

    celery.start()

而后启动celelry worker(要否则你的任务怎么执行呢?确定须要一个消费者取走):

 

celery -A tasks worker --loglevel=info

可是这里的问题也可能很严重:咱们的异步非阻塞依赖于celery,仍是这个队列的长度,假如任务不少那么就须要等待,效率很低.有没有一种办法把个人同步阻塞函数变为异步(或者说被tornado的装饰器理解和识别)呢?

 

#!/bin/env python

import tornado.httpserver

import tornado.ioloop

import tornado.options

import tornado.web

import tornado.httpclient

import tornado.gen

from tornado.concurrent import run_on_executor

# 这个并发库在python3自带在python2须要安装sudo pip install futures

from concurrent.futures import ThreadPoolExecutor

import time

from tornado.options import define, options

define("port", default=8000, help="run on the given port", type=int)

class SleepHandler(tornado.web.RequestHandler):

    executor = ThreadPoolExecutor(2)

  #executor 是局部变量  不是全局的

    @tornado.web.asynchronous

    @tornado.gen.coroutine

    def get(self):

        # 假如你执行的异步会返回值被继续调用能够这样(只是为了演示),不然直接yield就行

        res = yield self.sleep()

        self.write("when i sleep %s s" % res)

        self.finish()

    @run_on_executor

    def sleep(self):

        time.sleep(5)

        return 5

class JustNowHandler(tornado.web.RequestHandler):

    def get(self):

        self.write("i hope just now see you")

if __name__ == "__main__":

    tornado.options.parse_command_line()

    app = tornado.web.Application(handlers=[

            (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)])

    http_server = tornado.httpserver.HTTPServer(app)

    http_server.listen(options.port)

    tornado.ioloop.IOLoop.instance().start()

相关文章
相关标签/搜索