Added some fcntl-based locking code for !win32

* If fcntl can't be imported it will just let things slide.
This commit is contained in:
Athanasius committed 2021-01-18 21:09:11 +00:00
1 parent 5c09ef78f8
commit 0263dc628b
1 file changed
+25
+25
View File
@@ -56,6 +56,7 @@ if __name__ == "__main__":
print('no_other_instance_running(): Begin...')
if platform == 'win32':
# win32 doesn't have fcntl, so we have to use msvcrt
print('no_other_instance_running(): win32')
import msvcrt
@@ -78,6 +79,30 @@ if __name__ == "__main__":
journal_dir_lockfile.write(f"Path: {journal_dir}\nPID: {os_getpid()}\n")
else:
try:
import fcntl
except Exception:
print("Not on win32 and we have no fcntl, can't use a file lock! Allowing multiple instances!")
try:
fcntl.flock(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError as e:
print(f"BlockingIOError: Couldn't lock journal directory \"{journal_dir}\", assuming another process running\n{e}")
return False
except OSError as e:
print(f"OSError: Couldn't lock journal directory \"{journal_dir}\", assuming another process running\n{e}")
return False
except Exception as e:
print(f"other Exception: Couldn't lock journal directory \"{journal_dir}\", assuming another process running\n{e}")
return False
journal_dir_lockfile.write(f"Path: {journal_dir}\nPID: {os_getpid()}\n")
print('no_other_instance_running(): Done')
return True