gpt4 book ai didi

python - aiohttp 帖子的当前上传步骤

转载 作者:行者123 更新时间:2023-12-05 04:31:35 25 4
gpt4 key购买 nike

我想知道如何从 aiohttp post 方法获取当前的上传步骤。通常我会使用 get 方法在循环中拉取当前步骤,但如果主机不响应当前上传步骤,这将不起作用。那么有可能得到当前步骤吗?诸如“从 xx% 上传几乎完成”之类的内容。我的意思是等到上传完成很烦人

async def post_task():
archive = open("file")
session = aiohttp.ClientSession()
post = await session.post("upload_url", data=archive, ssl = False)
await post.read()
await session.close()


loop = asyncio.get_event_loop()
loop.run_until_complete(post_task())

最佳答案

您可以尝试使用 streaming uploads结合tqdm.asyncio跟踪文件上传的进度。

或多或少来自流式上传文档:

import asyncio
import os.path

import aiofiles as aiofiles
import aiohttp as aiohttp
from tqdm.asyncio import tqdm


async def file_sender(file_name, chunksize):
async with aiofiles.open(file_name, "rb") as f:
chunk = await f.read(chunksize)
while chunk:
yield chunk
chunk = await f.read(chunksize)


def upload_with_progress(file_name=None, chunksize=64 * 1024):
size = os.path.getsize(file_name)
return tqdm(file_sender(file_name, chunksize), total=size // chunksize)


# Then you can use file_sender as a data provider:
async def post_task():
async with aiohttp.ClientSession() as session:
async with session.post(
"upload_url",
data=upload_with_progress("file"),
ssl=False,
) as resp:
print(await resp.text())


loop = asyncio.get_event_loop()
loop.run_until_complete(post_task())

关于python - aiohttp 帖子的当前上传步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71827532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com