1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-21 11:27:38 +03:00

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 2021-01-18 16:41:50 +00:00
parent 5c09ef78f8
commit 0263dc628b

@ -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