Python Asyncio file.write 在 request.get(文件)不工作后
Python Asyncio file.write after request.get(file) not working
我正在使用 Asyncio 和 aiohttp 从端点异步获取文件。我的请求状态代码是成功的,但是当我尝试写入文件时,由于某种原因,一切总是空的。
这就是我的代码现在的样子:
async def download_link(url:str,my_session:ClientSession, filename:Path):
async with my_session.get(url, allow_redirects=True) as response:
with filename.open(mode='wb') as f: #Line 3
await f.write(response.content)
async def download_all(urls:list, filenames:list):
my_conn = aiohttp.TCPConnector(limit=10)
async with aiohttp.ClientSession(connector=my_conn) as session:
tasks = []
for item in zip(urls,file_names):
task = asyncio.ensure_future(download_link(url=item[0],my_session=session, filename=item[1]))
tasks.append(task)
await asyncio.gather(*tasks,return_exceptions=True)
我还尝试将 async
放在第 3 行的 with 前面,在 download_link 函数内。而且我还尝试制作打开文件并将其写入一个单独的异步函数的代码:
async def store_response(response, filename:Path):
async with filename.open(model='wb') as f:
f.write(response.content)
我知道我从中获取的文件确实有数据,当我使用多线程时我能够取回数据。有人知道为什么会这样吗?
我用这段代码异步下载文件没问题,速度也不错。
import asyncio
import aiohttp
import aiofile
async def download_file(url: str):
filename = url.split('?')[0].split('/')[-1]
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if not resp.ok:
print(f"Invalid status code: {resp.status}")
else:
try:
async with aiofile.async_open(filename, "wb+") as afp:
async for chunk in resp.content.iter_chunked(1024 * 512): # 500 KB
await afp.write(chunk)
except asyncio.TimeoutError:
print(f"A timeout ocurred while downloading '{filename}'")
asyncio.run(download_file("https://www.python.org/static/img/python-logo.png"))
我正在使用 Asyncio 和 aiohttp 从端点异步获取文件。我的请求状态代码是成功的,但是当我尝试写入文件时,由于某种原因,一切总是空的。
这就是我的代码现在的样子:
async def download_link(url:str,my_session:ClientSession, filename:Path):
async with my_session.get(url, allow_redirects=True) as response:
with filename.open(mode='wb') as f: #Line 3
await f.write(response.content)
async def download_all(urls:list, filenames:list):
my_conn = aiohttp.TCPConnector(limit=10)
async with aiohttp.ClientSession(connector=my_conn) as session:
tasks = []
for item in zip(urls,file_names):
task = asyncio.ensure_future(download_link(url=item[0],my_session=session, filename=item[1]))
tasks.append(task)
await asyncio.gather(*tasks,return_exceptions=True)
我还尝试将 async
放在第 3 行的 with 前面,在 download_link 函数内。而且我还尝试制作打开文件并将其写入一个单独的异步函数的代码:
async def store_response(response, filename:Path):
async with filename.open(model='wb') as f:
f.write(response.content)
我知道我从中获取的文件确实有数据,当我使用多线程时我能够取回数据。有人知道为什么会这样吗?
我用这段代码异步下载文件没问题,速度也不错。
import asyncio
import aiohttp
import aiofile
async def download_file(url: str):
filename = url.split('?')[0].split('/')[-1]
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if not resp.ok:
print(f"Invalid status code: {resp.status}")
else:
try:
async with aiofile.async_open(filename, "wb+") as afp:
async for chunk in resp.content.iter_chunked(1024 * 512): # 500 KB
await afp.write(chunk)
except asyncio.TimeoutError:
print(f"A timeout ocurred while downloading '{filename}'")
asyncio.run(download_file("https://www.python.org/static/img/python-logo.png"))