2023-05-11 18:55:48 +09:00
|
|
|
import os
|
|
|
|
from typing import IO, Any
|
|
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
import msvcrt
|
|
|
|
elif os.name == "posix":
|
|
|
|
import fcntl
|
|
|
|
|
|
|
|
|
|
|
|
class FileLock:
|
|
|
|
@staticmethod
|
|
|
|
def lock(file: IO[Any]) -> None:
|
|
|
|
if os.name == "nt":
|
|
|
|
msvcrt.locking(file.fileno(), msvcrt.LK_LOCK, 1)
|
|
|
|
elif os.name == "posix":
|
|
|
|
fcntl.flock(file.fileno(), fcntl.LOCK_EX)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def unlock(file: IO[Any]) -> None:
|
|
|
|
if os.name == "nt":
|
|
|
|
msvcrt.locking(file.fileno(), msvcrt.LK_UNLCK, 1)
|
2023-05-17 14:51:19 +09:00
|
|
|
elif os.name == "posix":
|
2023-05-11 18:55:48 +09:00
|
|
|
fcntl.flock(file.fileno(), fcntl.LOCK_UN)
|