def openLocked(path, mode="w"): if os.name == "posix": import fcntl f = open(path, mode) fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB) else: f = open(path, mode) return f