Why do I need to await twice

Viewed 49

I'm trying to run some IO blocking code, so I'm using to_thread to send the function to another thread. I tried several things, but in all cases, I seem to have to await the to_thread which just returns another coroutine (?!) that I then have to await again. For some reason this isn't quite clicking.

import asyncio

async def search(keyword_list):
    coroutines = set()
    for index, kw in enumerate(keyword_list):
            coroutines.add(asyncio.to_thread(do_lookup, keyword, index))

    for result in asyncio.as_completed(coroutines):
        outcome, index = await result
        # Do some magic with the outcome and index
        # BUT, this doesn't work because `await result` apparently
        # just returns ANOTHER coroutine!


async def do_lookup(keyword, index):
    # Do long, blocking stuff here
    print(f'running...{keyword} {index}')
    return keyword, index


if __name__ == '__main__':
    asyncio.run(search([1, 2, 3, 4]))
1 Answers

As I was copy/pasting and adapting my code to make a generic example, I discovered the problem here.

do_lookup is supposed to a synchronous function (because of the usage of to_thread), so by defining it async dev do_lookup I'm instead defining it as an asynchronous function, thereby causing the "double" await issue.

Simply redefining do_lookup without the async keyword did the trick!

Related