I'm new to Python async concepts. However I want to sequentially call 2 functions, the order is what matters here:
def func1():
p = subprocess.Popen('ffmpeg -i in.webm out.mp4')
p.wait()
def func2():
os.remove('in.webm')
func1()
func2()
Function 1 contains subprocess which converts video files, and function 2 deletes the input afterwards.
Popen with p.wait() does force synchronous execution but it comes at the price of blocking the main thread.
Popen without p.wait() doesn't block it but makes function 2 to get called before ffmpeg will complete its job.
I would like to achieve something similar to Javascript promises or async/await constructs like this:
async func1() {
let response = await fetch('/api');
let user = await response.json();
return user;
}
func2(func1());
Sounds like a trivial task but I'm lost between multithreading, multiprocessing and asyncio.
How can I make function 2 to await for the output of function 1 without blocking the main thread?