문제

I typed AsyncHTTPClient sample code from tornado's website in an interactive python interpreter but the asynchronous HTTP request is never executed.

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)

Can I use AsyncHTTPClient not as part of a web server processing?

도움이 되었습니까?

해결책

Yes, but you should start an IOLoop, example from 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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top