[Python] How to use asyncio with existing blocking library?

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have a few blocking functions foo, bar and I can't change those (Some internal library I don't control. Talks to one or more network services). How do I use it as async? E.g. I want to do the following:

    results = []
    for inp in inps:
    val = foo(inp)
    result = bar(val)
    results.append(result)


    This will be inefficient as I can call foo for the second input while I am waiting for the first and same for bar. How do I wrap them such that they are usable with asyncio (i.e new async, await syntax)?

    Lets assume the functions are re-entrant. i.e it is fine to call foo again when already a previous foo is processing.

    Continue reading...

Compartilhe esta Página