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

标签 python asynchronous tornado asynchttpclient python-interactive

我输入了AsyncHTTPClient sample code来自tornado网站的交互式Python解释器,但异步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 作为 Web 服务器处理的一部分吗?

最佳答案

是的,但您应该启动 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()

关于python - 如何在没有服务器的情况下立即执行 AsyncHTTPClient() (使用交互式解释器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801879/

相关文章:

python - anafast 如何处理Healpy 中的 mask ?

python 日历小部件 - 返回用户选择的日期

javascript - 异步返回问题

python - Tornado 在一个请求期间存储上下文,或获取当前请求处理程序?

Python/Tornado - 压缩静态文件

python - 删除值计数满足条件的列 (Pandas)

python - 如何在场景 QGraphicsScene 上居中小部件

java - 从解析的 XML 文件创建表时发生 Android 错误

java - 如何在 Java 中调用异步 Web 服务

javascript - 解析从 python(tornado) 到 javascript 的路径变量