import aiohttp import asyncio import time from aiohttp import FormData def read_config(file_name): config = {} with open(file_name, 'r') as f: lines = f.readlines() for line in lines: key, value = line.strip().split('=') if key in ["LAT", "LON", "REQUESTS"]: value = float(value) if "." in value else int(value) config[key] = value return config config = read_config('config.txt') URL = config["URL"] LAT = config["LAT"] LON = config["LON"] FILENAME = config["FILENAME"] FILE_TYPE = config["FILE_TYPE"] FILE_PATH = FILENAME + FILE_TYPE requests = config["REQUESTS"] async def make_request(session): data = FormData() data.add_field("data", '{"gps_x": "%s", "gps_y": "%s", "filename": "%s", "file_type": "%s"}' % (LAT, LON, FILENAME, FILE_TYPE), content_type='application/json') data.add_field('file', open(FILE_PATH, 'rb'), filename=FILE_PATH, content_type='image/png') response = await session.post(URL, data=data) print(response) return await response.json() async def main(): async with aiohttp.ClientSession() as session: tasks =[make_request(session) for _ in range(requests)] await asyncio.gather(*tasks) # tasks = [make_request(session) for _ in range(requests)] # responses = await asyncio.gather(*tasks) # for response in responses: # print(response) if __name__ == "__main__": time_init = time.time() asyncio.run(main()) time_finish = time.time() time_elapsed = time_finish - time_init print(f"for {requests}, it took {time_elapsed} \nTPS = {requests/time_elapsed}")