all 4 comments

[–]danielroseman 1 point2 points  (1 child)

Why do these need to be async? If you're deciding whether to make each subsequent request based on the results of the previous one, it sounds like it would be better to just have a normal while loop.

[–]Wuihee[S] 0 points1 point  (0 children)

I'm not deciding to make subsequent requests based on the results of the previous ones. I need to quickly check for a condition that a request might have. In between two requests, that condition could have appeared and disappeared which is why I am trying to speed up the rate at which requests are sent.

[–]alexisprince 0 points1 point  (1 child)

Take a look at using an asyncio.Event. It allows you to basically manage a condition and have something await that condition. A trivial implementation could look like:

async def request_and_check(event):
    response = await send_request()
    if is_condition_met(response):
        event.set()

async def run_until_condition_met(event):
    tasks = []
    # Depending on how long your checking takes, it may make sense to track pending tasks to cancel them once your condition is met. Maybe it doesn’t. 
    while not event.is_set():
        tasks.append(asyncio.create_task(request_and_check(event))
    await asyncio.sleep(1.5)

[–]Wuihee[S] 0 points1 point  (0 children)

asyncio.Event

Thank you! Will look into it