如何在没有服务器的情况下立即执行AsyncHTTPClient()(使用交互式解释器)

时间:2013-09-14 12:43:44

标签: python asynchronous tornado asynchttpclient python-interactive

我在一个交互式python解释器中从龙卷风的网站输入AsyncHTTPClient sample code但是从不执行异步HTTP请求。

def handle_request(response):
    if response.error:
        print "Error:", response.error
    else:
        print response.body

http_client = AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)

# handle_request function is never executed (nothing is printed)

我是否可以将AsyncHTTPClient用作网络服务器处理的一部分?

1 个答案:

答案 0 :(得分:3)

是的,但您应该从IOLoop开始docs示例:

from tornado import ioloop
from tornado.httpclient import AsyncHTTPClient


def handle_request(response):
    if response.error:
        print "Error:", response.error
    else:
        print response.body
    ioloop.IOLoop.instance().stop()

http_client = AsyncHTTPClient()
http_client.fetch("http://www.google.com/", handle_request)
ioloop.IOLoop.instance().start()