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

38 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"""
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()