python-3.x subprocess.run 没有超时,即使指定了超时

m528fe3b  于 2023-03-04  发布在  Python
关注(0)|答案(2)|浏览(199)

我有下面的Python代码:

strRunOutput = subprocess.run([strFastbootExecutable, "-s", dsn.upper(), "reboot"], timeout=5, stderr=subprocess.PIPE).stderr.decode('utf-8').upper()

基本上就是这样:

fastboot -s G070GV1871970FCW reboot

这是它的输出

< waiting for G070GV1871970FCW >

...这是挂起。为什么fastboot命令挂起,我不知道,但更困扰我的是,www.example.com命令没有超时后5秒,因为我告诉它,并导致我的程序挂起。任何想法是怎么回事?subprocess.run command isn't timing out after 5 seconds as I told it to and is causing my program to hang. Any ideas what's going on?
谢谢!

qmb5sa22

qmb5sa222#

问题是因为管道。然而,以下方法即使有管道也对我有效:

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
try:
  return proc.communicate(timeout=1.0)
except TimeoutExpired:
  proc.kill()
  return proc.communicate()

来源:https://github.com/python/cpython/issues/81605

相关问题