Domanda

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?

È stato utile?

Soluzione

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()
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top