`asyncio` 模块中是否有类似`CalledProcessError` 的东西?
Is there an analog to `CalledProcessError` in the `asyncio` module?
在 asyncio
模块中是否有模拟来自 subprocess
模块的 CalledProcessError
异常?
我原以为会有一个模拟,因为 asyncio
模块从 subprocess
模块创建了 TimeoutError
exception to replace the TimeoutExpired
异常。但是好像没有。
作为一个子问题:CalledProcessError
异常是否仅适用于 tornado.process
中实现(子)进程功能的 Popen
class? Or does it also work with other classes (e.g. Process
from asyncio
or Subprocess
的实例?
似乎 CalledProcessError
异常可能适用于 classes 的实例,但与 Popen
不同。例如,可以找到 GitHub examples 的人使用来自 tornado.process
.[=52 的 Subprocess
class 的实例调用 CalledProcessError
=]
请注意,在上面的示例中,虽然 tornado.process
确实具有 subprocess.CalledProcessError
的别名,但基于文件顶部的 import
语句不是正在调用的异常;真的是subprocess.CalledProcessError
被调用了
如果 CalledProcessError
也适用于 asyncio
Process
class 的实例,那么为什么 TimeoutExpired
也适用于 asyncio
的实例Process
class 来自 asyncio
?换句话说,在 asyncio
为什么需要创建 TimeoutExpired
异常的新版本 (TimeoutError
),但 不需要 创建CalledProcessError
异常的新版本?
归根结底,the source code for CalledProcessError
并没有那么复杂,所以如果我想为来自 [=10= 的 Process
class 的实例创建一个副本版本],我想我可能可以,但首先尝试避免此类代码重复似乎是可取的。
注意: 我目前的工作心智模型是“asyncio.subprocess
模块旨在完成 subprocess
模块所做的所有事情,但是是异步的”。就此心智模型不正确而言,此问题可能包含固有的 preconceptions/misconceptions.
asyncio
不包含 CalledProcessError
的任何等价物,因为它不包含引发 CalledProcessError
的事物的任何等价物。 check_call
、check_output
、check_returncode
或 run
的 check
参数没有等效项。
没有这样的例外,没有。 asyncio.subprocess
implementation should be seen as the equivalent of subprocess.Popen()
. From asyncio.subprocess.Process
documentation:
The API of the Process
class was designed to be close to the API of the subprocess.Popen
class[.]
CalledProcessError
异常仅由同一模块中的更高级别的函数(例如 suprocess.run()
)抛出,每个函数都在幕后驱动 Popen()
。
当 Process.returncode
value 非零时引发您自己的异常。如果你愿意,你可以为此重新使用 CalledProcessError
,它只是命令 运行、return 代码以及 stderr
和 [=21] 的容器=] 字符串。这些都是内置的 Python 类型,没有 subprocess
特定的类型。
在 asyncio
模块中是否有模拟来自 subprocess
模块的 CalledProcessError
异常?
我原以为会有一个模拟,因为 asyncio
模块从 subprocess
模块创建了 TimeoutError
exception to replace the TimeoutExpired
异常。但是好像没有。
作为一个子问题:CalledProcessError
异常是否仅适用于 tornado.process
中实现(子)进程功能的 Popen
class? Or does it also work with other classes (e.g. Process
from asyncio
or Subprocess
的实例?
似乎 CalledProcessError
异常可能适用于 classes 的实例,但与 Popen
不同。例如,可以找到 GitHub examples 的人使用来自 tornado.process
.[=52 的 Subprocess
class 的实例调用 CalledProcessError
=]
请注意,在上面的示例中,虽然 tornado.process
确实具有 subprocess.CalledProcessError
的别名,但基于文件顶部的 import
语句不是正在调用的异常;真的是subprocess.CalledProcessError
被调用了
如果 CalledProcessError
也适用于 asyncio
Process
class 的实例,那么为什么 TimeoutExpired
也适用于 asyncio
的实例Process
class 来自 asyncio
?换句话说,在 asyncio
为什么需要创建 TimeoutExpired
异常的新版本 (TimeoutError
),但 不需要 创建CalledProcessError
异常的新版本?
归根结底,the source code for CalledProcessError
并没有那么复杂,所以如果我想为来自 [=10= 的 Process
class 的实例创建一个副本版本],我想我可能可以,但首先尝试避免此类代码重复似乎是可取的。
注意: 我目前的工作心智模型是“asyncio.subprocess
模块旨在完成 subprocess
模块所做的所有事情,但是是异步的”。就此心智模型不正确而言,此问题可能包含固有的 preconceptions/misconceptions.
asyncio
不包含 CalledProcessError
的任何等价物,因为它不包含引发 CalledProcessError
的事物的任何等价物。 check_call
、check_output
、check_returncode
或 run
的 check
参数没有等效项。
没有这样的例外,没有。 asyncio.subprocess
implementation should be seen as the equivalent of subprocess.Popen()
. From asyncio.subprocess.Process
documentation:
The API of the
Process
class was designed to be close to the API of thesubprocess.Popen
class[.]
CalledProcessError
异常仅由同一模块中的更高级别的函数(例如 suprocess.run()
)抛出,每个函数都在幕后驱动 Popen()
。
当 Process.returncode
value 非零时引发您自己的异常。如果你愿意,你可以为此重新使用 CalledProcessError
,它只是命令 运行、return 代码以及 stderr
和 [=21] 的容器=] 字符串。这些都是内置的 Python 类型,没有 subprocess
特定的类型。