subprocess.run指定timeout仍未超时挂起问题求助
Let's dig into why your 5-second timeout isn't kicking in when running that fastboot reboot command, and how to fix it.
First, let's recap your setup:
Your Python code (note the missing parentheses on .upper() at the end—small typo!):
strRunOutput = subprocess.run([strFastbootExecutable, "-s", dsn.upper(), "reboot"], timeout=5, stderr=subprocess.PIPE).stderr.decode('utf-8').upper()
The actual command being executed:
fastboot -s G070GV1871970FCW reboot
And you're stuck with this output hanging indefinitely:
< waiting for G070GV1871970FCW >
Why the timeout isn't working as expected
The subprocess.run() timeout should raise a TimeoutExpired exception after 5 seconds, but there's a common gotcha with tools like fastboot: they often hang waiting on stdin even when they don't need user input. By default, subprocess.run() inherits stdin from your Python process, and if fastboot is holding that open, it can prevent Python from properly terminating the process when the timeout hits.
Another possibility: if you're not wrapping the call in a try/except block, the TimeoutExpired exception might be crashing your program silently, but you mentioned the program hangs—so that's less likely. The stdin issue is the more probable culprit here.
Fixes to get the timeout working reliably
Here are a few actionable steps to fix this:
Redirect stdin to DEVNULL
Closing stdin for the fastboot process removes that hanging point, letting Python's timeout signal work as intended. Update your code like this:import subprocess from subprocess import DEVNULL try: result = subprocess.run( [strFastbootExecutable, "-s", dsn.upper(), "reboot"], timeout=5, stderr=subprocess.PIPE, stdin=DEVNULL, stdout=subprocess.PIPE # Capture stdout too, just in case the waiting message is there ) strRunOutput = result.stderr.decode('utf-8').upper() # Fixed the .upper() typo here except subprocess.TimeoutExpired: print("Fastboot command timed out after 5 seconds") # Add cleanup logic here if neededUse Popen for more control
Ifsubprocess.run()still doesn't behave, switch tosubprocess.Popento manually kill the process on timeout. This is more explicit:import subprocess proc = subprocess.Popen( [strFastbootExecutable, "-s", dsn.upper(), "reboot"], stderr=subprocess.PIPE, stdin=DEVNULL, stdout=subprocess.PIPE ) try: stdout, stderr = proc.communicate(timeout=5) strRunOutput = stderr.decode('utf-8').upper() except subprocess.TimeoutExpired: proc.kill() # Force terminate the fastboot process # Grab any remaining output after killing stdout, stderr = proc.communicate() strRunOutput = stderr.decode('utf-8').upper() print("Fastboot timed out and was killed")Troubleshoot the underlying fastboot hang
While fixing the timeout is urgent, don't ignore why fastboot is hanging in the first place:- Verify the device is actually in fastboot mode with
fastboot devices - Try a different USB cable or port (flaky connections are common here)
- Ensure you have the correct USB drivers installed for your device
- Some devices require running fastboot with sudo/admin privileges
- Verify the device is actually in fastboot mode with
内容的提问来源于stack exchange,提问作者Mike

