Python并发之异步I/O(async,await)-4
- UID
- 1066743
|
Python并发之异步I/O(async,await)-4
例子2
例子1只是用来讲述语法,实际用途中还是得看这个例子。上节多线程与多进程的例子,获取有效网址。
import asyncio
import time
import aiohttp
import async_timeout
msg = "http://www.nationalgeographic.com.cn/photography/photo_of_the_day/{}.html"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}
urls = [msg.format(i) for i in range(4500, 5057)]
async def fetch(session, url):
with async_timeout.timeout(10):
async with session.get(url) as response:
return response.status
async def main(url):
async with aiohttp.ClientSession() as session:
status = await fetch(session, url)
return status
if __name__ == '__main__':
start = time.time()
loop = asyncio.get_event_loop()
tasks = [main(url) for url in urls]
# 返回一个列表,内容为各个tasks的返回值
status_list = loop.run_until_complete(asyncio.gather(*tasks))
print(len([status for status in status_list if status==200]))
end = time.time()
print("cost time:", end - start)
任重而道远
在封闭的代码块中使用一些新的关键字就能实现异步功能
我对于这一块还是处于小白状态,掌握不是很全面
多Google,多Google,多Google..... |
|
|
|
|
|