I'm wondering how I can get the current Upload step from the aiohttp post
method. Usually I would use the get
method to pull the current step in a loop but this doesn't work if the host doesn't response the current upload step. So it is possible to get the current step? Something like "upload from xx% are almost done". I mean it is very annoying to wait until the upload is completed
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())
You could try to use streaming uploads in combination with tqdm.asyncio
to track the progress of the file upload.
More or less from the streaming uploads documentation:
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())