迫于机器配置太低,用多进程多线程,一秒钟才处理几百条 uri ,于是想着来用异步协程来试下,看着文档写出了这样一个丑陋的代码,搞了几万条 uri 测试了下,好像也没啥问题,不打印结果到屏幕的话,一秒钟差不多可以处理 1000 条,大概有这么几个步骤:
我现在的困惑是:
耽误大佬周五下午一点点时间,帮忙瞅一眼,不胜感激!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import asyncio
from asyncio import Semaphore
from aiohttp import ClientSession
from itertools import islice
def get_lines_iterator(filename, n=1000):
with open(filename) as fp:
while True:
lines = list(islice(fp, n))
if lines:
yield lines
else:
break
async def delete_file(uri: str,
session: ClientSession,
sem: Semaphore) -> int:
headers = {'Authorization': 'xxxxxxxxxxx'}
url = api + uri
async with sem, session.delete(url, headers=headers) as response:
return uri, response.status
# 写法 1:
# async def main(uris):
# sem = Semaphore(100)
# async with ClientSession() as session:
# tasks = [delete_file(uri, session, sem) for uri in uris]
# await asyncio.gather(*tasks)
# 写法 2:
async def main(uris):
sem = Semaphore(100)
async with ClientSession() as session:
async with asyncio.TaskGroup() as group:
result = [group.create_task(delete_file(
uri, session, sem)) for uri in uris]
return result
if __name__ == '__main__':
for lines in get_lines_iterator("uris.txt"):
uris = [uri.strip() for uri in lines]
result = asyncio.run(main(uris))
for x in result:
print(x.result())
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.