来自 aiohhtp 的当前上传步骤 post

Current upload step from aiohhtp post

我想知道如何从 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.text()


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

您可以尝试使用 streaming uploads in combination with 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())