Claude Code for fcntl: Python File Descriptor and File Control — Claude Skills 360 Blog
Blog / AI / Claude Code for fcntl: Python File Descriptor and File Control
AI

Claude Code for fcntl: Python File Descriptor and File Control

Published: October 21, 2028
Read time: 5 min read
By: Claude Skills 360

Python’s fcntl module manipulates file descriptor properties and implements advisory file locking on Unix/macOS. import fcntl. fcntl: fcntl.fcntl(fd, cmd, arg=0) → int or bytes; commands: F_GETFL / F_SETFL (file status flags), F_GETFD / F_SETFD (fd flags, notably FD_CLOEXEC), F_DUPFD (dup to ≥ fd). ioctl: fcntl.ioctl(fd, request, arg=0) — device control. flock: fcntl.flock(fd, operation) — BSD-style whole-file advisory locks; operations: LOCK_SH (shared), LOCK_EX (exclusive), LOCK_UN (unlock), LOCK_NB (non-blocking, combine with |). lockf: fcntl.lockf(fd, cmd, len=0, start=0, whence=0) — POSIX byte-range locking; cmd: LOCK_SH, LOCK_EX, LOCK_UN. Non-blocking: fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK). Close-on-exec: fcntl.fcntl(fd, fcntl.F_SETFD, fcntl.FD_CLOEXEC). flock advisory: lock held until fd closed or explicitly unlocked; does not prevent other processes from reading or writing if they ignore the lock. Windows: not available — use msvcrt.locking() or portalocker (PyPI). Claude Code generates PID file guards, single-instance daemons, non-blocking pipe readers, and concurrent file access managers.

CLAUDE.md for fcntl

## fcntl Stack
- Stdlib: import fcntl, os
- NonBlocking: flags = fcntl.fcntl(fd, fcntl.F_GETFL)
               fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
- CloseOnExec: fcntl.fcntl(fd, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
- ExLock:  fcntl.flock(fd, fcntl.LOCK_EX)   # block until locked
- TryLock: fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)  # raises BlockingIOError
- Unlock:  fcntl.flock(fd, fcntl.LOCK_UN)

fcntl File Control Pipeline

# app/fcntlutil.py — non-blocking, close-on-exec, flock, lockf, pid file, single-instance
from __future__ import annotations

import contextlib
import errno
import os
import platform
import struct
import sys
from contextlib import contextmanager
from dataclasses import dataclass
from pathlib import Path
from typing import Generator

_FCNTL_AVAILABLE = platform.system() != "Windows"
if _FCNTL_AVAILABLE:
    import fcntl


# ─────────────────────────────────────────────────────────────────────────────
# 1. File descriptor flag helpers
# ─────────────────────────────────────────────────────────────────────────────

def set_nonblocking(fd: int) -> None:
    """
    Set a file descriptor to non-blocking mode.
    After this, read/write will raise BlockingIOError instead of blocking.

    Example:
        set_nonblocking(pipe_read_fd)
        try:
            data = os.read(pipe_read_fd, 1024)
        except BlockingIOError:
            data = b""   # no data available yet
    """
    if not _FCNTL_AVAILABLE:
        raise OSError("fcntl not available on Windows")
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)


def set_blocking(fd: int) -> None:
    """Restore a file descriptor to blocking mode."""
    if not _FCNTL_AVAILABLE:
        raise OSError("fcntl not available on Windows")
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    fcntl.fcntl(fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK)


def is_nonblocking(fd: int) -> bool:
    """Return True if the file descriptor is in non-blocking mode."""
    if not _FCNTL_AVAILABLE:
        return False
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    return bool(flags & os.O_NONBLOCK)


def set_cloexec(fd: int) -> None:
    """
    Set the close-on-exec flag so the fd is closed in child processes.
    Prevents leaking open file descriptors across exec() calls.

    Example:
        set_cloexec(socket.fileno())   # don't inherit socket in child
    """
    if not _FCNTL_AVAILABLE:
        raise OSError("fcntl not available on Windows")
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)


def clear_cloexec(fd: int) -> None:
    """Clear the close-on-exec flag so child processes inherit the fd."""
    if not _FCNTL_AVAILABLE:
        raise OSError("fcntl not available on Windows")
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    fcntl.fcntl(fd, fcntl.F_SETFD, flags & ~fcntl.FD_CLOEXEC)


def is_cloexec(fd: int) -> bool:
    """Return True if the close-on-exec flag is set."""
    if not _FCNTL_AVAILABLE:
        return False
    flags = fcntl.fcntl(fd, fcntl.F_GETFD)
    return bool(flags & fcntl.FD_CLOEXEC)


# ─────────────────────────────────────────────────────────────────────────────
# 2. Advisory file locking (flock)
# ─────────────────────────────────────────────────────────────────────────────

@contextmanager
def exclusive_lock(
    file_obj,
    blocking: bool = True,
) -> Generator[None, None, None]:
    """
    Acquire an exclusive (write) advisory lock on a file.
    blocking=False raises BlockingIOError if lock can't be acquired immediately.

    Example:
        with open("data.json", "r+") as f, exclusive_lock(f):
            data = json.load(f)
            data["count"] += 1
            f.seek(0); json.dump(data, f); f.truncate()
    """
    if not _FCNTL_AVAILABLE:
        yield   # no-op on Windows
        return
    op = fcntl.LOCK_EX | (0 if blocking else fcntl.LOCK_NB)
    fcntl.flock(file_obj, op)
    try:
        yield
    finally:
        fcntl.flock(file_obj, fcntl.LOCK_UN)


@contextmanager
def shared_lock(
    file_obj,
    blocking: bool = True,
) -> Generator[None, None, None]:
    """
    Acquire a shared (read) advisory lock on a file.
    Multiple readers can hold shared locks simultaneously.

    Example:
        with open("config.json") as f, shared_lock(f):
            data = json.load(f)
    """
    if not _FCNTL_AVAILABLE:
        yield
        return
    op = fcntl.LOCK_SH | (0 if blocking else fcntl.LOCK_NB)
    fcntl.flock(file_obj, op)
    try:
        yield
    finally:
        fcntl.flock(file_obj, fcntl.LOCK_UN)


def try_lock(file_obj) -> bool:
    """
    Try to acquire an exclusive lock without blocking.
    Returns True if locked, False if already locked by another process.

    Example:
        with open("resource.lock", "w") as f:
            if not try_lock(f):
                print("Another process holds the lock")
    """
    if not _FCNTL_AVAILABLE:
        return True
    try:
        fcntl.flock(file_obj, fcntl.LOCK_EX | fcntl.LOCK_NB)
        return True
    except BlockingIOError:
        return False


# ─────────────────────────────────────────────────────────────────────────────
# 3. PID file / single-instance guard
# ─────────────────────────────────────────────────────────────────────────────

class AlreadyRunningError(Exception):
    """Raised when another instance of the process is already running."""
    pass


class PidFile:
    """
    Single-instance guard using a PID file with exclusive lock.
    Holding the lock proves this process owns the PID file.

    Example:
        with PidFile("/var/run/myapp.pid") as pf:
            print(f"Running with PID {pf.pid}")
            main()
    """

    def __init__(self, path: str | Path) -> None:
        self._path = Path(path)
        self._file = None
        self.pid = os.getpid()

    def __enter__(self) -> "PidFile":
        self._path.parent.mkdir(parents=True, exist_ok=True)
        self._file = open(self._path, "w")
        if not try_lock(self._file):
            self._file.close()
            raise AlreadyRunningError(
                f"Another instance is running (lock held on {self._path})"
            )
        self._file.write(str(self.pid) + "\n")
        self._file.flush()
        return self

    def __exit__(self, *args) -> None:
        if self._file:
            fcntl.flock(self._file, fcntl.LOCK_UN)
            self._file.close()
        try:
            self._path.unlink(missing_ok=True)
        except OSError:
            pass

    @staticmethod
    def read_pid(path: str | Path) -> int | None:
        """Read the PID from an existing PID file, or None if absent/invalid."""
        try:
            text = Path(path).read_text().strip()
            return int(text)
        except (OSError, ValueError):
            return None


# ─────────────────────────────────────────────────────────────────────────────
# 4. Non-blocking pipe / socket reader
# ─────────────────────────────────────────────────────────────────────────────

def drain_nonblocking(fd: int, chunk: int = 4096) -> bytes:
    """
    Read all available bytes from a non-blocking fd without blocking.
    Returns empty bytes if no data is ready.

    Example:
        set_nonblocking(pipe_fd)
        while True:
            data = drain_nonblocking(pipe_fd)
            if data: process(data)
    """
    buf: list[bytes] = []
    while True:
        try:
            chunk_data = os.read(fd, chunk)
            if not chunk_data:
                break
            buf.append(chunk_data)
        except BlockingIOError:
            break
        except OSError as e:
            if e.errno == errno.EAGAIN:
                break
            raise
    return b"".join(buf)


@contextmanager
def nonblocking_fd(fd: int) -> Generator[int, None, None]:
    """
    Context manager that temporarily makes fd non-blocking.

    Example:
        with nonblocking_fd(pipe_read) as fd:
            data = drain_nonblocking(fd)
    """
    if not _FCNTL_AVAILABLE:
        yield fd
        return
    was_blocking = not is_nonblocking(fd)
    set_nonblocking(fd)
    try:
        yield fd
    finally:
        if was_blocking:
            set_blocking(fd)


# ─────────────────────────────────────────────────────────────────────────────
# 5. FD info inspector
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class FdInfo:
    fd:           int
    is_open:      bool
    is_nonblock:  bool
    is_cloexec:   bool
    flags_hex:    str

    def __str__(self) -> str:
        flags: list[str] = []
        if self.is_nonblock: flags.append("nonblock")
        if self.is_cloexec:  flags.append("cloexec")
        return (f"fd={self.fd}  open={self.is_open}  "
                f"flags={self.flags_hex}  [{', '.join(flags) or 'default'}]")


def fd_info(fd: int) -> FdInfo:
    """
    Return information about an open file descriptor.

    Example:
        print(fd_info(sys.stdout.fileno()))
    """
    if not _FCNTL_AVAILABLE:
        return FdInfo(fd=fd, is_open=True, is_nonblock=False, is_cloexec=False, flags_hex="0x0")
    try:
        fl_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
        fd_flags = fcntl.fcntl(fd, fcntl.F_GETFD)
        return FdInfo(
            fd=fd,
            is_open=True,
            is_nonblock=bool(fl_flags & os.O_NONBLOCK),
            is_cloexec=bool(fd_flags & fcntl.FD_CLOEXEC),
            flags_hex=f"{fl_flags:#010x}",
        )
    except OSError:
        return FdInfo(fd=fd, is_open=False, is_nonblock=False, is_cloexec=False, flags_hex="0x0")


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import json, tempfile

    print("=== fcntl demo ===")

    if not _FCNTL_AVAILABLE:
        print("  fcntl not available on Windows — skipping")
        raise SystemExit(0)

    # ── fd_info: examine stdin/stdout/stderr ───────────────────────────────────
    print("\n--- fd_info ---")
    for fd, name in [(0, "stdin"), (1, "stdout"), (2, "stderr")]:
        print(f"  {name}: {fd_info(fd)}")

    # ── non-blocking pipe ──────────────────────────────────────────────────────
    print("\n--- non-blocking pipe ---")
    read_fd, write_fd = os.pipe()
    try:
        # Write some data
        os.write(write_fd, b"hello fcntl")
        # Read non-blocking
        with nonblocking_fd(read_fd) as fd:
            data = drain_nonblocking(fd)
        print(f"  drained: {data!r}")
        # Verify nothing left
        with nonblocking_fd(read_fd) as fd:
            empty = drain_nonblocking(fd)
        print(f"  second drain (empty): {empty!r}")
    finally:
        os.close(read_fd)
        os.close(write_fd)

    # ── set_cloexec ────────────────────────────────────────────────────────────
    print("\n--- cloexec ---")
    r, w = os.pipe()
    try:
        print(f"  pipe read fd before: cloexec={is_cloexec(r)}")
        set_cloexec(r)
        print(f"  pipe read fd after:  cloexec={is_cloexec(r)}")
    finally:
        os.close(r); os.close(w)

    # ── exclusive_lock ─────────────────────────────────────────────────────────
    print("\n--- exclusive_lock ---")
    with tempfile.TemporaryDirectory() as tmpdir:
        data_file = os.path.join(tmpdir, "counter.json")
        # Initialize
        with open(data_file, "w") as f:
            json.dump({"count": 0}, f)

        # Update with exclusive lock
        with open(data_file, "r+") as f, exclusive_lock(f):
            data = json.load(f)
            data["count"] += 1
            f.seek(0)
            json.dump(data, f)
            f.truncate()

        with open(data_file) as f:
            result = json.load(f)
        print(f"  counter after locked update: {result['count']}")

        # try_lock demo
        with open(data_file, "w") as f:
            locked = try_lock(f)
            print(f"  try_lock (no competition): {locked}")
            if locked:
                fcntl.flock(f, fcntl.LOCK_UN)

    # ── PidFile ────────────────────────────────────────────────────────────────
    print("\n--- PidFile ---")
    with tempfile.TemporaryDirectory() as tmpdir:
        pid_path = os.path.join(tmpdir, "app.pid")
        try:
            with PidFile(pid_path) as pf:
                print(f"  acquired PID file: pid={pf.pid}")
                read_pid = PidFile.read_pid(pid_path)
                print(f"  PidFile.read_pid: {read_pid}")
                # Attempt to acquire a second instance
                try:
                    with PidFile(pid_path):
                        print("  ERROR: second instance should not succeed")
                except AlreadyRunningError as e:
                    print(f"  second instance blocked: {type(e).__name__}")
        except AlreadyRunningError:
            print("  first instance already running?")

    print("\n=== done ===")

For the portalocker alternative — portalocker (PyPI) provides the same advisory locking API (portalocker.lock(), portalocker.unlock()) with cross-platform support including Windows via msvcrt.locking() — use portalocker when you need file locking on Windows or want a single API that works everywhere; use fcntl.flock() or fcntl.lockf() on Unix when you want zero dependencies and are confident you’re only targeting Linux/macOS. For the multiprocessing.Lock / threading.Lock alternative — threading.Lock and multiprocessing.Lock provide in-process and inter-process synchronization through kernel semaphores or pipes, but do not prevent a separate unrelated process (e.g. a daemon script restarted by cron) from accessing a shared file — use fcntl.flock() as the shared file guard between independently launched Unix processes; use threading/multiprocessing locks for coordinating concurrent access within a single program. The Claude Skills 360 bundle includes fcntl skill sets covering set_nonblocking()/set_blocking()/is_nonblocking()/set_cloexec()/clear_cloexec()/is_cloexec() fd flag helpers, exclusive_lock()/shared_lock()/try_lock() advisory lock context managers, AlreadyRunningError/PidFile single-instance daemon guard, drain_nonblocking()/nonblocking_fd() non-blocking pipe reader, and FdInfo with fd_info() inspector. Start with the free tier to try file locking patterns and fcntl pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free