代码
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.gen
from tornado.httpclient import AsyncHTTPClient,HTTPRequest
class Test1Handler(tornado.web.RequestHandler):
NEED_SLEEP = False
@tornado.gen.coroutine
def get(self):
http_client = AsyncHTTPClient()
http_request = HTTPRequest(url="http://localhost:8899/test2", method='GET', request_timeout=120)
response = yield http_client.fetch(http_request) #这个是异步处理的动作
#每次间隔的访问,使用tornado的睡眠机制 睡眠5秒中
if Test1Handler.NEED_SLEEP:
yield tornado.gen.sleep(20) #这个是异步处理的动作
Test1Handler.NEED_SLEEP = not Test1Handler.NEED_SLEEP
# 完全转发报文头部和body
for h in response.headers:
self.set_header(h, response.headers[h])
return self.finish(response.body)
class Test2Handler(tornado.web.RequestHandler):
SEQ = 0
def get(self):
Test2Handler.SEQ += 1
print(Test2Handler.SEQ)
return self.finish('TEST2 {0}'.format(Test2Handler.SEQ))
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/test1", Test1Handler),
(r"/test2", Test2Handler),
]
tornado.web.Application.__init__(self, handlers)
if __name__ == "__main__":
port = 8899
application = Application()
http_server = tornado.httpserver.HTTPServer(application, xheaders=True)
http_server.listen(port)
print('Listen on http://localhost:{0}'.format(port))
tornado.ioloop.IOLoop.instance().start()
向"/test1"发送请求时,Test1Handler 使用 AsyncHTTPClient 异步的http客户端转发给了"/test2",并把test2的完整结果全部回复给了"/test1"的请求方,使用浏览器打开多个标签页都访问 http://localhost:8899/test1,就可以看到效果了。
tornado使用装饰器@tornado.gen.coroutine,来表明该请求里面可能有异步处理, 需要进行异步处理的动作前面会用yield来说明。
我在Test1Handler里面使用 yield tornado.gen.sleep(20) 来进行异步睡眠的操作, 这个睡眠操作不会阻塞整个python进程,tornado继续处理其他的请求。
这个异步操作的方法极大的提高了效率,在单线程的python中,当你写爬虫操作的时候,就不需要等待前面一个请求有返回值之后再发送另外一个请求,而是同时发送好几个请求,当其中的某些请求正常返回的时候tornado会自动进行下一步操作。
其他异步方法
1.异步睡眠
yield tornado.gen.sleep(20)
这个睡眠不会阻塞python的线程,睡眠时tornado服务仍然在正常的运行
2.异步调用
IOLoop.instance().add_callback(callback, *args, **kwargs)
将callback加入到tornado的加入到的处理队列里面。
IOLoop.instance().call_later(delay, callback, *args, **kwargs)
延时delay秒之后,将callback加入到tornado的加入到的处理队列里面
还有其他异步调用的方法:
http://www.tornadoweb.org/en/stable/ioloop.html#running-an-ioloop
3.定时调用
http://www.tornadoweb.org/en/stable/ioloop.html#tornado.ioloop.PeriodicCallback
tornado.ioloop.PeriodicCallback(callback, callback_time, io_loop=None)
callback设定定时调用的方法
callback_time设定每次调用之间的间隔
值得注意的是每次调用之间没有任何关系,如果上次的调用还没有处理完,本次的调用仍然会继续跑, 可以通过继承这个类,然后加入一些判断机制,来保证一次调用处理完,才能进行下一次调用。