异步执行Python subprocess.Popen with wait()

Asynchronous execution of Python subprocess.Popen with wait()

我是 Python 异步概念的新手。但是我想顺序调用2个函数,顺序在这里很重要:

def func1():
    p = subprocess.Popen('ffmpeg -i in.webm out.mp4')
    p.wait()

def func2():
    os.remove('in.webm')

func1()
func2()

函数 1 包含转换视频文件的子进程,函数 2 之后删除输入。
Popenp.wait() 确实强制同步执行,但它是以阻塞主线程为代价的。
Popen 没有 p.wait() 不会阻止它,但会在 ffmpeg 完成其工作之前调用函数 2。

我想实现类似于 Javascript promises 或 async/await 构造的东西,如下所示:

async func1() {
    let response = await fetch('/api');
    let user = await response.json();
    return user;
}

func2(func1());

听起来像是一项微不足道的任务,但我在多线程、多处理和异步之间迷失了方向。
如何在不阻塞主线程的情况下让函数 2 等待函数 1 的输出?

要在 asyncio 程序中生成子进程,请使用 asyncio's subprocess primitives,它提供类似于 subprocess 的接口,但公开了可等待的协程。

您的 JavaScript 示例的等价物如下所示:

async def main():
    # Schedule func1() to run, but don't await it. (In JavaScript
    # it would be enough to just call func1().)
    asyncio.create_task(func1())

    print('we get here immediately')

    # Await something to keep the event loop spinning - without
    # this, the program would exit at this point. In real code
    # this would await an exit signal, server.wait_closed(), etc.
    await asyncio.Event().wait()

async def func1():
    # Like in JavaScript, await suspends the current function
    # until a result is available.
    res = await fetch('/api1', {})
    userid = (await res.json()).id
    asyncio.create_task(func2(userid))

async def func2(userid):
    res = await fetch('/api2', ...userid...)
    func3((await res.json()).url)

def func3(url):
    print('user photo', url)

asyncio.run(main())