我现在的困惑是:
耽误大佬周五下午一点点时间,帮忙瞅一眼,不胜感激!
#!/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())