Python-100-Days/Day16-20/code/example24.py

38 lines
1.2 KiB
Python
Raw Permalink Normal View History

2019-01-25 02:52:40 +08:00
"""
aiohttp - 异步HTTP网络访问
异步I/O异步编程- 只使用一个线程单线程来处理用户请求
用户请求一旦被接纳剩下的都是I/O操作通过多路I/O复用也可以达到并发的效果
这种做法与多线程相比可以让CPU利用率更高因为没有线程切换的开销
Redis/Node.js - 单线程 + 异步I/O
Celery - 将要执行的耗时间的任务异步化处理
异步I/O事件循环 - uvloop
"""
import asyncio
import re
import aiohttp
async def fetch(session, url):
async with session.get(url, ssl=False) as resp:
return await resp.text()
async def main():
pattern = re.compile(r'\<title\>(?P<title>.*)\<\/title\>')
urls = ('https://www.python.org/',
'https://git-scm.com/',
'https://www.jd.com/',
'https://www.taobao.com/',
'https://www.douban.com/')
async with aiohttp.ClientSession() as session:
for url in urls:
html = await fetch(session, url)
print(pattern.search(html).group('title'))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()