1

I would like to call an async function from a pure sync code. I would like to execute that async function in the background without stucking my prog. My idea is to use the threading module.

from threading import Thread
import asyncio

async def func1():
    ...

def func2():
    ...

if __name__ == '__main__':
    Thread(target=func1).start()
    Thread(target=func2).start()

Any idea? Thanks in advance!

1 Answer 1

1

Since Python 3.7 there is asyncio.run.

Replace

    Thread(target=func1).start()

by

    Thread(target=asyncio.run, args=(func1(),)).start()
Sign up to request clarification or add additional context in comments.

1 Comment

I have been searching for this exactly for 3 days :) I tried similar ideas but they didn't work , asyncio.run is the solution , thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.