trio.open_process 和 trio.run_process 给出属性错误

trio.open_process and trio.run_process give attribute errors

我正在尝试创建一个异步子进程并将一个变量传递给标准输入,但是 trio.run_process 和 trio.open_process 都给出了一个属性错误。它没有指定导致错误的原因。

Traceback (most recent call last):
  File "./update.py", line 122, in pkgUpdates
    trio.run(self.asetup, password)
  File "/home/user/.local/lib/python3.7/site-packages/trio/_core/_run.py", line 1444, in run
    raise runner.main_task_outcome.error
  File "./update.py", line 65, in asetup
    nursery.start_soon(self.upProc, password, 'update')
  File "/home/user/.local/lib/python3.7/site-packages/trio/_core/_run.py", line 506, in __aexit__
    raise combined_error_from_nursery
  File "./update.py", line 75, in upProc
    await trio.open_process(['sudo', '-S', 'apt-get', cmd, '-y'],
  File "/home/user/.local/lib/python3.7/site-packages/trio/_deprecate.py", line 125, in __getattr__
    raise AttributeError(name)
AttributeError: open_process

我也尝试过将 trio.Process 与 stdin=io.BytesIO(password) 一起使用,但这会导致 io.UnsupportedOperation 错误。仅传递字节也会出错。

函数是:

async def upProc(self, password, cmd):
    await trio.open_process(['sudo', '-S', 'apt-get', cmd, '-y'],
                   stdin=password.encode(), capture_stdout=True)

    if (cmd == 'update'):
        await self.upProc(password, 'upgrade')
    return

您在 open_processrun_process 上获得 AttributeError 的原因是它们仅在 Trio v0.12.0 中添加,您可能使用的是 v0.11.0 .这不是你的错——我大约 20 分钟前才发布 v0.12.0 :-)。但我猜你正在查看开发版本的文档,这让你失望了。

假设你可以升级到 v0.12.0,我想你想要:

await trio.run_process(['sudo', '-S', 'apt-get', cmd, '-y'], stdin=password_bytes)

这会启动进程,等待它完成,然后 returns 一些关于进程的信息,全部都一次性完成。