speed up files uploading using aiofiles & aiohttp?

104 views Asked by At

I need to speed up files uploading script. Please, advise how to fix error Enable tracemalloc to get the object allocation traceback or there's already known way to do it differently?

import asyncio
import aiofiles
import aiohttp


async def upload_file(session, local_path):
    file_data = {
        'me': 'yo'
    }

    async with aiofiles.open(local_path, 'rb') as fp:
        file_content = await fp.read()

        response = session.post('http:/my_url', data=file_content, json=file_data)


async def upload_files(paths):
    async with aiohttp.ClientSession() as session:
        await asyncio.gather(*[upload_file(session, **path) for path in paths])


async def main():
    await upload_files([
        {'local_path': '1.txt'},
    ])


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
1

There are 1 answers

1
RomanPerekhrest On BEST ANSWER

Await for ClientSession.post request:

response = await session.post('http:/my_url', data=file_content, json=file_data)