e768098d0e
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
Flake8 Lint / flake8 (push) Has been cancelled
Spell check CI / Spell_Check (push) Has been cancelled
28 lines
667 B
Python
28 lines
667 B
Python
import contextlib
|
|
import os
|
|
import sys
|
|
|
|
if sys.platform.startswith("win"):
|
|
import msvcrt
|
|
else:
|
|
import fcntl
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def acquire_lock(filename):
|
|
if not sys.platform.startswith("win"):
|
|
with open(filename, "a+") as f:
|
|
fcntl.flock(f, fcntl.LOCK_EX)
|
|
yield f
|
|
fcntl.flock(f, fcntl.LOCK_UN)
|
|
else: # Windows
|
|
with open(filename, "w") as f:
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
|
|
yield f
|
|
msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
|
|
|
|
try:
|
|
os.remove(filename)
|
|
except OSError:
|
|
pass # best effort to remove the lock file
|