[Python] How to call async function from sync funcion and get result, while a loop is already...

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a asyncio running loop, and from the coroutine I'm calling a sync function, is there any way we can call and get result from an async function in a sync function tried below code, it is not working want to print output of hel() in i() without changing i() to async function is it possible, if yes how?

    import asyncio

    async def hel():
    return 4

    def i():
    loop = asyncio.get_running_loop()
    x = asyncio.run_coroutine_threadsafe(hel(), loop) ## need to change
    y = x.result() ## this lines
    print(y)

    async def h():
    i()

    asyncio.run(h())

    Continue reading...

Compartilhe esta Página