【发布时间】:2015-04-07 15:43:03
【问题描述】:
我正在使用 Asyncio 和 Requests 对一系列 HTTP 请求进行基准测试。
出于某种原因,使用 Asyncio 比直接使用请求要慢一些。知道为什么吗?我是否错误地使用了 Asyncio?
import asyncio
import functools
import requests
import time
ts = time.time()
for i in range(10):
@asyncio.coroutine
def do_checks():
loop = asyncio.get_event_loop()
req = loop.run_in_executor(None, functools.partial(requests.get, "http://google.com", timeout=3))
resp = yield from req
print(resp.status_code)
loop = asyncio.get_event_loop()
loop.run_until_complete(do_checks())
te = time.time()
print("Version A: " + str(te - ts))
ts = time.time()
for i in range(10):
r = requests.get("http://google.com", timeout=3)
print(r.status_code)
te = time.time()
print("Version B: " + str(te - ts))
输出:
版本 A = 异步;版本 B = 请求
200
200
200
200
200
200
200
200
200
200
Version A: 5.7215821743011475
200
200
200
200
200
200
200
200
200
200
Version B: 5.320340156555176
【问题讨论】:
标签: python performance python-3.x benchmarking python-asyncio