Skip to content

Threads and Processes

Real apps do several things at once: web servers use thread pools, data pipelines use worker processes. Your logs should keep up, without mixed-up lines or lost messages.

Here's how Logust behaves with threads, with processes, and on free-threaded Python.

Threads

The logger is thread-safe. You can log from as many threads as you want, to the same sinks, without any locking on your side:

import threading

from logust import logger

logger.remove()
logger.add("threads.log", format="{thread.name} | {message}", mode="w")


def worker():
    for i in range(1000):
        logger.info("Processed item {}", i)


threads = [threading.Thread(target=worker, name=f"worker-{n}") for n in range(4)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

logger.complete()

with open("threads.log") as f:
    lines = f.read().splitlines()
print(len(lines), "lines, for example:", lines[-1])
$ python main.py

4000 lines, for example: worker-3 | Processed item 999

Four threads, 1,000 messages each: 4,000 complete lines in the file. Each message is written as a whole line, lines from different threads are never mixed together.

{thread.name} in the format (or record["thread"].name in a filter) tells you which thread logged each line. Give your threads names, it makes the logs much easier to read. 😎

Context in threads and tasks

contextualize() is context-local: its values live in a contextvars variable, so each thread and each asyncio task sees only the blocks it entered itself. Two threads, or two tasks in asyncio.gather(), can each open their own block at the same time, and their values never mix.

There are a few details about what a new thread, a new task or a generator gets.

New threads start empty

A new threading.Thread does not inherit the values of the block it was started in. If you want it to, run its target with contextvars.copy_context().run():

import contextvars
import sys
import threading

from logust import logger

logger.remove()
logger.add(sys.stderr, format="{thread.name} | {message} | {extra}")


def work():
    logger.info("Working")


with logger.contextualize(job_id="j-42"):
    logger.info("Starting the threads")

    plain = threading.Thread(target=work, name="plain")
    plain.start()
    plain.join()

    context = contextvars.copy_context()
    copied = threading.Thread(target=context.run, args=(work,), name="copied")
    copied.start()
    copied.join()
$ python main.py

MainThread | Starting the threads | {'job_id': 'j-42'}
plain | Working | {}
copied | Working | {'job_id': 'j-42'}

plain starts with an empty context. copied runs work() inside a copy of the main thread's context, so it gets the job_id.

Technical Details

This is how contextvars work in Python, not something specific to Logust. Python copies the context for new threads by itself when sys.flags.thread_inherit_context is set (Python 3.14+, on by default on free-threaded builds).

Thread pools like concurrent.futures.ThreadPoolExecutor don't pass the context of submit() to the function they run either. Submit contextvars.copy_context().run with your function as its first argument, or open the block inside the function the pool runs.

Tasks inherit, and keep, the values

An asyncio task created inside a block inherits its values, because asyncio copies the current context when it creates a task. And it keeps them, even after the block that created it has ended:

import asyncio
import sys

from logust import logger

logger.remove()
logger.add(sys.stderr, format="{message} | {extra}")


async def send_email():
    await asyncio.sleep(0.01)
    logger.info("Email sent")


async def main():
    with logger.contextualize(order_id=1234):
        task = asyncio.create_task(send_email())
        logger.info("Order saved")
    logger.info("Back in main()")
    await task


asyncio.run(main())
$ python main.py

Order saved | {'order_id': 1234}
Back in main() | {}
Email sent | {'order_id': 1234}

"Email sent" is logged after main() has left the block, and it still has the order_id: the task got its own copy of the context when it was created. 🤓

It also works the other way: blocks that a task opens itself never affect its parent or the other tasks.

Generators share their caller's context

A generator runs in the context of the code that iterates it. So a block left open across a yield is visible to the caller too, until the generator resumes and exits the block. That's the same as in loguru:

import sys

from logust import logger

logger.remove()
logger.add(sys.stderr, format="{message} | {extra}")


def read_rows():
    with logger.contextualize(source="orders.csv"):
        yield "row 1"
        yield "row 2"


for row in read_rows():
    logger.info("Got {}", row)

logger.info("Done")
$ python main.py

Got row 1 | {'source': 'orders.csv'}
Got row 2 | {'source': 'orders.csv'}
Done | {}

The "Got ..." lines are logged by the for loop, not by read_rows(), but they have the source, because the generator's block is still open while it is paused at yield. When the generator finishes, its block exits, and "Done" has no context.

If you don't want that, open the block around the code that uses the values, without a yield inside it.

Performance

A plain logger.info("msg") inside a block costs the same as outside of it. The merged context is cached per thread, so logging inside a block stays on the fast Rust path.

Processes

Each process has its own logger. What a child process starts with depends on how it's created:

  • With spawn (the default on Windows and macOS) or forkserver (the default on Linux since Python 3.14), the child starts a fresh interpreter. It imports logust again and gets the default logger, not your handlers. So configure logging in the child too.
  • With fork, the child gets a copy of the parent's logger, handlers included.

The portable way is to configure logging in a function that both the parent and the workers call:

import multiprocessing
from pathlib import Path

from logust import logger


def setup_logging():
    logger.remove()
    logger.add("workers.log", format="{process.name} | {message}", enqueue=True)


def work(task_id):
    setup_logging()
    for step in range(500):
        logger.info("Task {} step {}", task_id, step)
    logger.complete()


if __name__ == "__main__":
    Path("workers.log").unlink(missing_ok=True)
    setup_logging()

    ctx = multiprocessing.get_context("spawn")
    processes = [ctx.Process(target=work, args=(n,), name=f"Worker-{n}") for n in range(3)]
    for process in processes:
        process.start()
    for process in processes:
        process.join()

    logger.info("All workers finished")
    logger.complete()

    with open("workers.log") as f:
        lines = f.read().splitlines()
    print(len(lines), "lines")
    print(lines[0])
    print(lines[-1])
$ python main.py

1501 lines
Worker-1 | Task 1 step 0
MainProcess | All workers finished

Three worker processes and the parent write to the same file, and every line is there: 3 × 500 from the workers, plus the parent's.

A few things to notice:

  • setup_logging() calls logger.remove() first, so the child doesn't keep the default console handler.
  • Each worker calls logger.complete() before it ends, so its queued messages are written before the process exits (see Async Writes).
  • {process.name} shows which process wrote each line.

Info

Several processes can append to the same log file. Logust coordinates them with a lock file next to the log (workers.log.lock here), so rotation also works when several processes share a file: one process rotates, and the others follow.

Fork caveats

Forking a process that has threads is delicate in any library, and logging uses threads for enqueue=True sinks. Logust handles the common cases:

  • A forked child can log through the sinks it inherited, and add and remove its own.
  • A child created with fork() never starts a writer thread: enqueue=True sinks inherited from the parent write synchronously in the child.
  • On macOS, enqueue=True sinks added in the forked child also write synchronously. Rust's thread parking uses libdispatch there, which crashes a forked child (SIGTRAP) once the parent has used it.
  • A child doesn't write the parent's buffered messages a second time.

Tip

If you can, prefer the spawn start method, and configure logging in each process. It works the same on every platform, and avoids the classic fork-with-threads pitfalls, in Logust and in every other library you use.

Free-threaded Python

Python 3.13 introduced an optional free-threaded build (python3.15t), without the GIL, where threads really run in parallel.

Logust supports it: release automation builds cp314t and cp315t wheels for Linux, macOS and Windows, and the test suite runs on 3.14t and 3.15t in CI. Importing logust keeps the GIL disabled.

You can check it with this script, which logs from 8 threads in parallel:

import sys
import sysconfig
import threading

from logust import logger

free_threaded = bool(sysconfig.get_config_var("Py_GIL_DISABLED"))
gil_enabled = getattr(sys, "_is_gil_enabled", lambda: True)()
version = sys.version.split()[0]
print(f"Python {version}, free-threaded build: {free_threaded}, GIL enabled: {gil_enabled}")

logger.remove()
logger.add("free-threaded.log", format="{thread.name} | {message}", mode="w")


def worker():
    for i in range(2000):
        logger.info("Processed item {}", i)


threads = [threading.Thread(target=worker) for _ in range(8)]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

logger.complete()
with open("free-threaded.log") as f:
    print(sum(1 for _ in f), "lines")
$ python3.15t main.py

Python 3.15.0, free-threaded build: True, GIL enabled: False
16000 lines

The GIL is still off after import logust, and all 16,000 messages are in the file. 🎉

Install it as usual, pip picks the free-threaded wheel for a free-threaded interpreter:

$ python3.15t -m pip install logust

---> 100%

Note

Free-threaded release builds target Python 3.14t and 3.15t. Python 3.13t is no longer supported by PyO3. If a wheel is unavailable for your platform, pip builds Logust from source, which needs a Rust toolchain.

Recap

  • The logger is thread-safe, lines from different threads never get mixed.
  • contextualize() is context-local: each thread and task sees only its own blocks.
  • New threads start without the values, use contextvars.copy_context().run() to pass them. Tasks inherit them, generators share their caller's.
  • With spawn or forkserver, configure logging in each process. With fork, the child inherits the handlers.
  • Several processes can share a log file, rotation included.
  • In forked children, enqueue=True sinks write synchronously.
  • Logust supports free-threaded Python 3.14t and 3.15t, and keeps the GIL disabled.