I have a list of file paths and their resumable upload links for uploading to GCS buckets. I implemented this normally and then using asyncio, and find no improvements in the speed of execution. Would appreciate any inputs, thanks.
from asyncio import run, gather
import requests
async def uploadFile(UPLOAD_URL, LOCAL_PATH):
with open(LOCAL_PATH, 'rb') as f:
data = f.read()
requests.put(UPLOAD_URL, data=data)
async def uploadFiles(path_and_url):
uploads = [uploadFile(dic['upload_url'], dic['local_path']) for dic in path_and_url]
await gather(*uploads)
run(uploadFiles(path_and_url))
Using async functions in the uploadFile option speeds up my code by about 15%, anything further I can do? Thanks!
import aiohttp, aiofiles
async def uploadFile(UPLOAD_URL, LOCAL_PATH):
async with aiofiles.open(LOCAL_PATH, 'rb') as f:
data = await f.read()
# httpx.put(UPLOAD_URL, data=data)
async with aiohttp.ClientSession() as session:
async with session.put(UPLOAD_URL, data=data) as resp:
print(f"{LOCAL_PATH} -> {resp}")
async def uploadFiles(path_and_url):
uploads = [uploadFile(dic['upload_url'], dic['local_path']) for dic in path_and_url]
await gather(*uploads)