Checking for file existence on potentially hanging NFS mountsPosted: | More posts about python
NFS mounts can be annoying as they tend to "hang" (for various reasons). Here I show how I replaced os.path.exists(..) with a better solution for potentially hanging paths.
The following code will "hang" if the NFS mount /mnt/nfs-shr is unresponsive:
I could not find any easy solution using the Python standard modules. But what about using a small timeout? In my case I just want to print "OK" if the file exists and can be read. Using the subprocess32 module with timeouts and a reasonable command (test) works.
The subprocess32 module is a backport of features found in subprocess of Python 3 to use on 2.x. One of the best features of subprocess32 is the timeout parameter for the Popen calls.
Installing the module as always:
The resulting code looks like:
from subprocess32 import check_call try: check_call(['test', '-f', '/mnt/nfs-shr/file.txt'], timeout=0.5) except: pass # ignore non-zero return code and timeout exceptions else: print 'OK, file exists.' # .. do important stuff
This worked good enough for me.