From 484659fc3f68306c79b8eabcad2c104e37e7d39e Mon Sep 17 00:00:00 2001 From: shortcutme Date: Mon, 7 Nov 2016 22:34:12 +0100 Subject: [PATCH] Separate lock file instad of log file locking --- src/main.py | 5 +++-- src/util/helper.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index f0b48cdb..dc94b765 100644 --- a/src/main.py +++ b/src/main.py @@ -44,7 +44,8 @@ if config.action == "main": from util import helper log_file_path = "%s/debug.log" % config.log_dir try: - helper.openLocked(log_file_path, "a") + lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w") + lock.write("%s" % os.getpid()) except IOError as err: print "Can't lock %s file, your ZeroNet client is probably already running, exiting... (%s)" % (log_file_path, err) sys.exit() @@ -55,7 +56,7 @@ if config.action == "main": os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir) logging.basicConfig( format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s', - level=logging.DEBUG, stream=helper.openLocked(log_file_path, "a") + level=logging.DEBUG, stream=open(log_file_path, "a") ) else: log_file_path = "%s/cmd.log" % config.log_dir diff --git a/src/util/helper.py b/src/util/helper.py index c46a9042..3b5126dd 100644 --- a/src/util/helper.py +++ b/src/util/helper.py @@ -39,6 +39,10 @@ def openLocked(path, mode="w"): import fcntl f = open(path, mode) fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) + elif os.name == "nt": + import msvcrt + f = open(path, mode) + msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, -1) else: f = open(path, mode) return f