My use case is to run some performance tests so I wanted to create an app where I run 1 task 4 times, compute the time average for that task, then run 2 tasks asynchronously, compute the average, then run 4 tasks asynchronously, compute the average, then 8 and so on.
However, I am not able to run like this. When I do, all tasks it seems have been executed before and I get wrong times.
I tried some hit and trial and with the below code now I get TypeError: An asyncio.Future, a coroutine or an awaitable is required
sys:1: RuntimeWarning: coroutine 'go' was never awaited
on line loop.run_until_complete(asyncio.wait(asyncio.ensure_future(some_tasks)))
in run_tasks
function.
Below is my code:
async def go(date):
pool = await aiopg.create_pool("**db connection**")
async with pool.acquire() as conn:
async with conn.cursor() as cur:
await cur.execute(""" some query """)
time.sleep(1)
ret = []
async for row in cur:
ret.append(row)
def date_range(date1, date2):
for n in range(int((date2 - date1).days)+1):
yield date1 + timedelta(n)
def run_tasks():
start_dt = datetime(2017, 8, 9)
end_dt = datetime(2017, 8, 10)
tasks = []
some_tasks = []
avg_time_run = []
for dt in date_range(start_dt, end_dt):
#tasks.append(asyncio.ensure_future(go(dt.strftime("%Y-%m-%d %H:%M:%S"))))
tasks.append(go(dt.strftime("%Y-%m-%d %H:%M:%S")))
i = 1
prev = 0
while i < 2: # i < 128
# Get i number of tasks from task list
for k in range(prev, i):
some_tasks.append(tasks[k])
prev = len(some_tasks)
time_run = []
for j in range(0, 4): # repeat task 4 times
start = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(asyncio.ensure_future(some_tasks)))
# loop.close()
end = time.time()
diff = end - start
time_run.append(diff)
print("ith SomeTask: {}, {}".format(i, some_tasks))
print("Total time: {}".format(diff))
# get average of each task run 4 times
avg_time_run.append(sum(time_run) / float(len(time_run)))
i *= 2
return avg_time_run
print(run_tasks())
Some hints will be appreciated. Where should I put await as it's there as asyncio.wait