From 0263dc628b52051acc7506f879c23ae3426bc58f Mon Sep 17 00:00:00 2001 From: Athanasius Date: Mon, 18 Jan 2021 16:41:50 +0000 Subject: [PATCH] Added some fcntl-based locking code for !win32 * If fcntl can't be imported it will just let things slide. --- EDMarketConnector.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/EDMarketConnector.py b/EDMarketConnector.py index 55fd4bd5..c271ce3a 100755 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -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