Skip to content
Snippets Groups Projects
Commit efb54981 authored by Geels, Brendan Ryan's avatar Geels, Brendan Ryan :tophat:
Browse files

Make fcntl package optional in Util.py

parent 8fb54718
No related branches found
No related tags found
No related merge requests found
......@@ -4,7 +4,12 @@ from obspy.core import Stats, Trace
from obspy import UTCDateTime
from io import BytesIO
import json
import fcntl
import warnings
try:
import fcntl
except:
pass
class ObjectView(object):
......@@ -270,6 +275,10 @@ def write_state_file(filename, data, directory=None, encoder=None):
fcntl.flock(f, fcntl.LOCK_EX)
json.dump(data, f, default=encoder)
fcntl.flock(f, fcntl.LOCK_UN)
except NameError as e:
print(
f"The fcntl module is not supported in Windows. Reading/writing state files will not work: {e}"
)
except IOError as e:
print(f"Error locking or writing to file: {e}")
raise
......@@ -281,6 +290,9 @@ def write_state_file(filename, data, directory=None, encoder=None):
raise
write_state_file("testjunkfile", "dataaaaa", "/home/bgeels/")
def read_state_file(filename, directory=None, decoder=None):
"""
Reads data from a state file in a thread-safe manner.
......@@ -318,6 +330,10 @@ def read_state_file(filename, directory=None, decoder=None):
data = json.load(f, object_hook=decoder)
fcntl.flock(f, fcntl.LOCK_UN)
return data
except NameError as e:
print(
f"The fcntl module is not supported in Windows. Reading/writing state files will not work: {e}"
)
except IOError as e:
print(f"Error locking or reading from file: {e}")
raise
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment