@asyncio.coroutine
def http_get(url): #
<span style="white-space:pre"> </span>nonlocal semaphore
<span style="white-space:pre"> </span>with (yield from semaphore):
<span style="white-space:pre"> </span>response = yield from aiohttp.request('GET', url)
<span style="white-space:pre"> </span>body = yield from response.content.read()
<span style="white-space:pre"> </span>yield from response.wait_for_close()
<span style="white-space:pre"> </span>return body
return http_get
tasks = [http_client(url) for url in urls]
for future in asyncio.as_completed(tasks):
<span style="white-space:pre"> </span>data = yield from future
loop = asyncio.get_event_loop()
result = loop.run_until_complete(run_experiment(base_url, num_iter))High Performance Python 笔记(Python是门不错的语言,全栈程序员就用它好了!)
原文地址:http://blog.csdn.net/cteng/article/details/39861787