On this page
fcntl
Prev: faulthandler | Up: stdlib_module | Next: filecmp
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — fcntl
Tags: #public #python3 #stdlib
Import
import fcntl
Overview
Low-level POSIX file-descriptor control (flock, lockf, ioctl, fcntl) for advisory file locking across processes and terminal/device ioctl queries (such as querying or setting PTY window size with TIOCGWINSZ/TIOCSWINSZ).
Quick Start
import fcntl
import tempfile
with tempfile.TemporaryFile() as fh:
fcntl.flock(fh.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
Implementation Walkthrough
1. Cross-Process Exclusive File Lock (flock)
Prevent two cron jobs or CLI runs from corrupting a shared state file simultaneously:
from contextlib import contextmanager
import fcntl
from pathlib import Path
@contextmanager
def file_lock(lock_path: Path):
lock_path.parent.mkdir(parents=True, exist_ok=True)
with lock_path.open("a+", encoding="utf-8") as fh:
fcntl.flock(fh.fileno(), fcntl.LOCK_EX)
try:
yield fh
finally:
fcntl.flock(fh.fileno(), fcntl.LOCK_UN)
2. Querying / Setting PTY Window Size (ioctl + struct)
import fcntl
import struct
import termios
def set_pty_size(fd: int, rows: int, cols: int) -> None:
winsize = struct.pack("HHHH", rows, cols, 0, 0)
fcntl.ioctl(fd, termios.TIOCSWINSZ, winsize)
Gotchas & Boundaries
- POSIX-only: Not available on Windows (use
msvcrt.lockingon Windows). flock()locks are advisory: All cooperating processes must acquire the same lock; processes that ignoreflock()can still read or write the file.- Avoid
LOCK_NBwithout catchingBlockingIOError: Non-blocking lock acquisition raisesBlockingIOErrorimmediately if another process holds the lock.
Common Functions
- fcntl.fcntl() —
fcntl(fd, cmd, arg=0, /)— Perform the operation cmd on file descriptor fd. - fcntl.flock() —
flock(fd, operation, /)— Perform the lock operation on file descriptor fd. - fcntl.ioctl() —
ioctl(fd, request, arg=0, mutate_flag=True, /)— Perform the operation request on file descriptor fd. - fcntl.lockf() —
lockf(fd, cmd, len=0, start=0, whence=0, /)— A wrapper around the fcntl() locking calls.
Constants & Attributes
fcntl.FASYNC(int) =64fcntl.FD_CLOEXEC(int) =1fcntl.F_DUPFD(int) =0fcntl.F_DUPFD_CLOEXEC(int) =67fcntl.F_FULLFSYNC(int) =51fcntl.F_GETFD(int) =1fcntl.F_GETFL(int) =3fcntl.F_GETLEASE(int) =107fcntl.F_GETLK(int) =7fcntl.F_GETNOSIGPIPE(int) =74fcntl.F_GETOWN(int) =5fcntl.F_GETPATH(int) =50fcntl.F_NOCACHE(int) =48fcntl.F_OFD_GETLK(int) =92fcntl.F_OFD_SETLK(int) =90
Linked from
Related notes
- tuts/python3/stdlib_module/faulthandler Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/filecmp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fcntl/fcntl Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fcntl/flock Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fcntl/ioctl Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fcntl/lockf Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib