S
S
S1riyS2021-12-26 14:35:48
Python
S1riyS, 2021-12-26 14:35:48

Why does the total query execution time grow too fast as the number of queries increases?

I use the Wandbox API in my project , sending requests asynchronously.

The code:

import time
import aiohttp
import asyncio

URL = 'https://wandbox.org/api/compile.json'
HEADERS = {'Content-Type': 'application/json'}

code = '''
class Compiler{
    public static void main(String[] args){
        System.out.println("This works!");
    }
}
'''

data = {
    'code': code,
    'compiler': 'openjdk-head',
    'stdin': ''
}


async def post(session):
    async with session.post(url=URL, headers=HEADERS, json=data) as wandbox_response:
        result = await wandbox_response.json()
        return result


async def gather_tasks(N):
    async with aiohttp.ClientSession() as session:
        tasks = []

        for i in range(N):
            tasks.append(asyncio.ensure_future(post(session=session)))

        result = await asyncio.gather(*tasks)

    return result


for number_of_tasks in range(1, 10):
    start_time = time.time()

    loop = asyncio.get_event_loop()  # Creating async loop
    results = loop.run_until_complete(gather_tasks(number_of_tasks))

    print(number_of_tasks, time.time() - start_time)


If you run it, then the console will display:
61c850d594351993139035.png

If you change the compiler, for example, to python and send the corresponding code:
code = '''
print('This works!')
'''

data = {
    'code': code,
    'compiler': 'cpython-head',
    'stdin': ''
}

Then the console will display:
61c8518c5119f787279323.png

Why is this happening and how to fix it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question