Answer the question
In order to leave comments, you need to log in
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)
code = '''
print('This works!')
'''
data = {
'code': code,
'compiler': 'cpython-head',
'stdin': ''
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question