1 Star 0 Fork 1

jiangpengjuj/ant-learn-python-concurrent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
10. async_spider_semaphore.py 669 Bytes
一键复制 编辑 原始数据 按行查看 历史
cztps2 提交于 2021-01-10 23:48 +08:00 . asyncio semaphore
import asyncio
import aiohttp
import blog_spider
semaphore = asyncio.Semaphore(10)
async def async_craw(url):
async with semaphore:
print("craw url: ", url)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
result = await resp.text()
await asyncio.sleep(5)
print(f"craw url: {url}, {len(result)}")
loop = asyncio.get_event_loop()
tasks = [
loop.create_task(async_craw(url))
for url in blog_spider.urls]
import time
start = time.time()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
print("use time seconds: ", end - start)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jiangpengjuj/ant-learn-python-concurrent.git
git@gitee.com:jiangpengjuj/ant-learn-python-concurrent.git
jiangpengjuj
ant-learn-python-concurrent
ant-learn-python-concurrent
main

搜索帮助