7

I need to open a file which is NFS mounted to my server. Sometimes, the NFS mount fails in a manner that causes all file operations to deadlock. In order to prevent this, I need a way to let the open function in python time out after a set period. E.g. something like open('/nfsdrive/foo', timeout=5). Of course, the default open procedure has no timeout or similar keyword.

Does anyone here know of a way to effectively stop trying to open a (local) file if the opening takes too long?

Note: I've already tried the urllib2 module, but it's timeout options only work for web requests, not local ones.

Pi Marillion
  • 4,465
  • 1
  • 19
  • 20
  • 1
    You could try starting a timer that, when expired, sends your very process a signal -- catch that signal and deal with the failure, or stop the timer if the `open` works. Not sure that solution would work on Windows if that's your platform -- it's a pretty Unixy approach... – Alex Martelli Jan 07 '15 at 15:24
  • Depending on the OS, there is a timeo parameter in /etc/fstab. I suggest you kook at your man pages for that file. – cdarke Jan 07 '15 at 15:32
  • 2
    If running on unix, you can use O_NONBLOCK flag with low level `os.open` to open the file and use `select.select` to wait the file ready with timeout. – Binux Jan 07 '15 at 16:13
  • https://stackoverflow.com/questions/21429369/read-file-with-timeout-in-python has some other suggestions for this type of problem, including `select` examples. – Dave Brondsema Jan 07 '19 at 20:07

1 Answers1

4

You can try using stopit

from stopit import SignalTimeout as Timeout

with Timeout(5.0) as timeout_ctx:
    with open('/nfsdrive/foo', 'r') as f:
        # do something with f
        pass

There may be some issues with SignalTimeout in multithreaded environments (like Django). ThreadingTimeout on the other hand may cause problems with resources on some virtual hostings when you run too many "time-limited" functions

P.S. My example also limits processing time of opened file. To only limit file opening you should use different approach with manual file opening/closing and manual exception handling

minghua
  • 5,981
  • 6
  • 45
  • 71
imposeren
  • 4,142
  • 1
  • 19
  • 27