如何摆脱Python子进程的重复超时异常消息?

时间:2017-07-24 08:53:53

标签: python exception-handling timeout subprocess error-messaging

此测试代码

import subprocess
out=subprocess.run(["sleep","10"], timeout=1)

在Python 3.5.3下生成以下输出:

Traceback (most recent call last):
  File "/usr/lib/python3.5/subprocess.py", line 385, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/usr/lib/python3.5/subprocess.py", line 801, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.5/subprocess.py", line 1476, in _communicate
    self.wait(timeout=self._remaining_time(endtime))
  File "/usr/lib/python3.5/subprocess.py", line 1385, in wait
    raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command '['sleep', '10']' timed out after 0.9999505979940295 seconds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/subprocess.py", line 390, in run
    stderr=stderr)
subprocess.TimeoutExpired: Command '['sleep', '10']' timed out after 1 seconds

这两个异常消息中的第一个是不恰当和混乱的。它包含Python API的普通用户不希望看到的内部。它表明在 subprocess.py中存在错误的,尽管预期子进程的常规行为会静默处理应用程序员强加的超时。

有没有办法获得更清晰的异常消息?

我是否应该在Python问题跟踪器上提出此投诉?

0 个答案:

没有答案