diff --git a/changes.md b/changes.md index c0c971a..57f1dae 100644 --- a/changes.md +++ b/changes.md @@ -1,6 +1,7 @@ ## v0.1.16 - 2017-07-07 - Translate RuntimeError of older versions of the netCDF4 library to IOError. +- Avoid race conditions when creating directories for new files. ## v0.1.15 - 2017-06-20 diff --git a/pynetcf/base.py b/pynetcf/base.py index 72c4901..e148c97 100644 --- a/pynetcf/base.py +++ b/pynetcf/base.py @@ -33,6 +33,7 @@ """ import os +import time import numpy as np import netCDF4 import datetime @@ -108,9 +109,7 @@ def __init__(self, filename, name=None, file_format="NETCDF4", if self.mode == "a" and not os.path.exists(self.filename): self.mode = "w" if self.mode == 'w': - path = os.path.dirname(self.filename) - if not os.path.exists(path): - os.makedirs(path) + self._create_file_dir() try: self.dataset = netCDF4.Dataset(self.filename, self.mode, @@ -121,6 +120,20 @@ def __init__(self, filename, name=None, file_format="NETCDF4", self.dataset.set_auto_scale(self.autoscale) self.dataset.set_auto_mask(self.automask) + def _create_file_dir(self): + """ + Create directory for file to sit in. + Avoid race condition if multiple instances are + writing files into the same directory. + """ + path = os.path.dirname(self.filename) + if not os.path.exists(path): + try: + os.makedirs(path) + except OSError: + time.sleep(1) + self._create_file_dir() + def _set_global_attr(self): """ Write global attributes to NetCDF file.