Skip to content

Commit

Permalink
provided alternative to fcntl for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfidan committed Mar 5, 2024
1 parent 4efa1c3 commit 1fbc1f0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 71 deletions.
54 changes: 28 additions & 26 deletions src/pyrad_proc/pyrad/io/read_data_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,20 +1254,21 @@ def read_monitoring_ts(fname, sort_by_date=False):
else:
raise
else:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
else:
raise
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise

# first count the lines
reader = csv.DictReader(
Expand Down Expand Up @@ -1408,20 +1409,21 @@ def read_intercomp_scores_ts(fname, sort_by_date=False):
else:
raise
else:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
else:
raise
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise

# first count the lines
reader = csv.DictReader(
Expand Down
141 changes: 96 additions & 45 deletions src/pyrad_proc/pyrad/io/write_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2052,20 +2052,21 @@ def write_monitoring_ts(start_time, np_t, values, quantiles, datatype, fname,
else:
raise
else:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
else:
raise
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise

csvfile.write('# Weather radar monitoring timeseries data file\n' +
'# Comment lines are preceded by "#"\n' +
Expand Down Expand Up @@ -2125,20 +2126,21 @@ def write_monitoring_ts(start_time, np_t, values, quantiles, datatype, fname,
else:
raise
else:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
else:
raise
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise

fieldnames = ['date', 'NP', 'central_quantile', 'low_quantile',
'high_quantile']
Expand Down Expand Up @@ -2278,15 +2280,38 @@ def write_intercomp_scores_ts(start_time, stats, field_name, fname,

if not file_exists:
with open(fname, 'w', newline='') as csvfile:
while True:
try:
fcntl.flock(csvfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except OSError as e:
if e.errno != errno.EAGAIN:
raise
else:
time.sleep(0.1)
if FCNTL_AVAIL:
while True:
try:
fcntl.flock(csvfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except OSError as e:
if e.errno == errno.EAGAIN:
time.sleep(0.1)
elif e.errno == errno.EBADF:
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise
else:
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise


csvfile.write(
'# Weather radar intercomparison scores timeseries file\n' +
Expand Down Expand Up @@ -2329,15 +2354,38 @@ def write_intercomp_scores_ts(start_time, stats, field_name, fname,
csvfile.close()
else:
with open(fname, 'a', newline='') as csvfile:
while True:
try:
fcntl.flock(csvfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except OSError as e:
if e.errno != errno.EAGAIN:
raise
else:
time.sleep(0.1)
if FCNTL_AVAIL:
while True:
try:
fcntl.flock(csvfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
break
except OSError as e:
if e.errno == errno.EAGAIN:
time.sleep(0.1)
elif e.errno == errno.EBADF:
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise
else:
while True:
try:
# Attempt to acquire an exclusive lock on the file
msvcrt.locking(os.open(csvfile, os.O_RDWR | os.O_CREAT), msvcrt.LK_NBLCK, 0)
break
except OSError as e:
if e.errno == 13: # Permission denied
time.sleep(0.1)
elif e.errno == 13: # No such file or directory
warn(
"WARNING: No file locking is possible (NFS mount?), " +
"expect strange issues with multiprocessing...")
break
else:
raise


fieldnames = ['date', 'NP', 'mean_bias', 'median_bias',
'quant25_bias', 'quant75_bias', 'mode_bias', 'corr',
Expand All @@ -2360,7 +2408,10 @@ def write_intercomp_scores_ts(start_time, stats, field_name, fname,
'intercep_of_linear_regression_of_slope_1': (
intercep_slope_1[i])
})
fcntl.flock(csvfile, fcntl.LOCK_UN)
if FCNTL_AVAIL:
fcntl.flock(csvfile, fcntl.LOCK_UN)
else:
msvcrt.locking(csvfile.fileno(), msvcrt.LK_UNLCK, os.path.getsize(csvfile))
csvfile.close()

return fname
Expand Down

0 comments on commit 1fbc1f0

Please sign in to comment.