Skip to content

Commit

Permalink
Avoid os.makedirs race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Paulik committed Jul 18, 2017
1 parent 8bc5a09 commit f920825
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 16 additions & 3 deletions pynetcf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"""

import os
import time
import numpy as np
import netCDF4
import datetime
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit f920825

Please sign in to comment.