SRCco.de

Checking for file existence on potentially hanging NFS mounts

Posted:   |  More posts about python
../galleries/python-logo.png

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:

if os.path.exists('/mnt/nfs-shr/file.txt'):
    print 'OK, file exists.' # .. do important stuff

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:

sudo pip install subprocess32

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.