This happens with NFS, if you set up an NFS share and the server is no longer available on the network, it often happens that any IO access to the loop simply gets stuck. If you try to run ls in the terminal, you will see that it just got stuck, does not fail, it just waits for an endless answer that it will never get. So, I recommend that you execute the ls command in your Python code and then set the timeout. Once this timeout is reached, you can throw an exception. Below is the implementation I tested. call_timeout is the function you give to the command you want to execute and the wait period in seconds. If the command completes before the timeout, it returns immediately, otherwise it kills the process that it spawned and raises an OSError that you can catch. Instead, you could choose True or False, this is a design choice. Some sample calls are listed below.
code
from subprocess import Popen, check_output import time def call_timeout(cmd, timeout): start = time.time() p = Popen(cmd) while time.time() - start < timeout: if p.poll() is not None: return time.sleep(0.1) p.kill() raise OSError('command timed out')
Call Examples
this should return without errors:
call_timeout(["sleep", "1"], 0.5)
this will throw an OSError error:
call_timeout(["sleep", "1"], 1.5)
you need something in this direction:
call_timeout(["ls", "/path/to/mountpoint"], 5.0)
source share