0

I want to execute two tasks forever which are run in parallel by asyncio.gather(). The first task contains some logic, for which I want to wait in the while True: loop. The second task is asyncio.sleep() which is responsible for execution of my tasks in equal timestamps. I want to get the result of the first task in asyncio.gather() just after it is finished.

async def main():
async with aiohttp.ClientSession() as session:
    while True:
        awaitingTime = calcAwaitingTime()
        data, _ = await asyncio.gather(BinanceClient.getLastSample(session), asyncio.sleep(awaitingTime))

if __name__ == '__main__':
loop = asyncio.get_event_loop()
task = loop.create_task(main())
loop.run_forever()

I want to get the result of the BinanceClient.getLastSample() just after it is finished not to wait after asyncio.sleep() also is finished. Maybe do you propose other way of executing these two tasks forever to have better possibility to get the results of them?

  • This sounds very similar to the problem in your [other question](https://stackoverflow.com/questions/53978919/even-using-asyncio-and-aiohttp-methods-wait-for-the-request-response). Did you try the approach from the answer you got there? – user4815162342 Jan 05 '19 at 11:40
  • The other question was about executing coroutines in parallel. And it can be obtained by using asyncio.gather(). In the question above I wanted to get the result returned by the first task. Anyway I made a workaround for this issue, by executing by logic task(getLastItem) and asyncio.sleep() sequentially, but each time I calculate awaiting time to execute the logic task in equal time frames. – Jack Wilkinson Jan 05 '19 at 11:54
  • If the issue is resolved, please post a response (and accept it), so that the question doesn't remain unanswered. My answer would suggest `gather(your_coroutine(session), sleep(60))` with `your_coroutine` being an `async def` that awaits `getLastSample` and processes it. – user4815162342 Jan 05 '19 at 12:05

0 Answers0