
440
|
附录
C
t2 = time.time()
print(f"{(t2-t1):.04}\t{len(r.content)}\t{url}")
async def get_sites(sites):
t1 = time.time()
async with trio.open_nursery() as nursery:
for url in sites:
nursery.start_soon(get_one, url, t1)
if __name__ == "__main__":
print("seconds\tbytes\turl")
trio.run(get_sites, urls)
结果如下:
$
python trio_asks_sites.py
seconds bytes url
0.1287 5735 https://exampleurl1
0.2134 146082 https://exampleurl4
0.215 11029 https://exampleurl2
0.3813 52385 https://exampleurl3
注意,
trio
并没有使用
asyncio.run()
,而是使用了自己的
trio.open_nursery()
。如果
感到好奇,可以读读文章“
What is the core dif
ference between asyncio and trio?
”以及关于
trio
设计决策的讨论。
有一个叫作
AnyIO
的新包提供了
asyncio ...