Skip to content

Async Writes

By default, a file sink writes in the thread that logs the message. Writing to a file is fast most of the time, but disks and network file systems sometimes stall. And while the write waits, so does your code.

With enqueue=True, the logging call only puts the message in a queue, and a background thread writes it to the file.

Enable async writes

Pass enqueue=True to logger.add():

from logust import logger

logger.remove()
logger.add("app.log", enqueue=True)

for i in range(3):
    logger.info("Processed batch {}", i)

logger.complete()

with open("app.log") as f:
    print(f.read(), end="")
$ python main.py

2026-10-06 12:06:10.061 | INFO     | __main__:<module>:7 - Processed batch 0
2026-10-06 12:06:10.061 | INFO     | __main__:<module>:7 - Processed batch 1
2026-10-06 12:06:10.061 | INFO     | __main__:<module>:7 - Processed batch 2

Everything else stays the same: the format, rotation and retention, JSON... enqueue=True only changes who writes to the file.

Note

enqueue applies to file sinks. For console and callable sinks, it is ignored, and they keep writing in the calling thread.

complete(): wait for the writes

Did you notice the logger.complete() in the example?

With a queue, the message is not in the file yet when logger.info() returns. logger.complete() waits until every pending message has been written to the files, and flushes them.

You can see the difference:

from logust import logger


def count_lines(path):
    with open(path) as f:
        return sum(1 for _ in f)


logger.remove()
logger.add("app.log", enqueue=True)

logger.info("Order received")
print("Before complete():", count_lines("app.log"), "lines")

logger.complete()
print("After complete():", count_lines("app.log"), "lines")
$ python main.py

Before complete(): 0 lines
After complete(): 1 lines

Right after logger.info(), the file is still empty. After logger.complete(), the line is there. ✨

Call logger.complete():

  • Before you read your own log files, like in tests.
  • Before your program exits, at shutdown. When Python exits normally, Logust writes what's left on its own (from an atexit handler). But an exit that skips cleanup, like os._exit() or a process killed by a signal, loses what is still in the queue: calling complete() during your shutdown code makes sure it's written before that point.
  • Before you hand over a log file to someone else, for example before uploading it.

logger.remove() also writes the pending messages of the handlers it removes.

Info

Sync file sinks (the default) also keep a small in-memory buffer (8 KB), to avoid one system call per message, and logger.complete() writes it too. So the advice above is good for every file sink, not only with enqueue=True. Use buffering=1 for a sync sink that writes each line before the logging call returns, see When it's written.

How the queue works

A few details that are good to know:

  • Each enqueue=True sink has its own background writer thread.
  • The writer batches lines, and every line reaches the file at most 100 ms after it was logged, even when messages keep coming. So tail -f app.log keeps up without complete().
  • That batching is the trade-off: if the process is killed, the messages of the last ~100 ms, and anything still in the queue, are lost. Only a sync sink with buffering=1 doesn't have such a window.
  • The queue holds up to 10,000 messages. If it's full, because the disk can't keep up, the logging call waits for room instead of dropping messages.
  • If the writer fails to write (disk full, permissions...), the error is printed to stderr, whatever the catch= setting is. With a queue, there's no logging call left to raise it from.

Sync or async?

Logust's sync writes are already fast: the formatting and writing happen in Rust, and the writes are buffered. So you don't need enqueue=True to go fast. It's about not blocking.

enqueue=False (default) enqueue=True
Who writes The calling thread A background thread
A slow disk... ...slows down the logging call ...is absorbed by the queue
Errors Can be reported or raised by the logging call Printed to stderr by the writer thread
Process killed (SIGTERM, os._exit(), crash) Up to 8 KB of lines can be lost, nothing with buffering=1 The last ~100 ms of messages can be lost
Good for Most apps, scripts, CLIs Web servers, high-throughput services, slow or network storage

Here's the time spent in the main thread for 10,000 messages, as the median of three runs of the benchmark suite (benchmarks/bench_throughput.py):

Library Sync Async
loguru 86.60 ms 370.86 ms
logust 8.25 ms 9.06 ms

In that run, loguru's enqueue=True path was slower than its sync path, while Logust's async path stayed close to its sync latency. Your numbers will depend on your machine and disk, see Benchmarks to reproduce them.

Tip

Use enqueue=True when a stalled disk must never stall your requests. For a script or a CLI, the default is simpler, and errors surface where they happen. With enqueue=True, buffering is ignored.

Forked processes

A process created with fork() never starts a writer thread: enqueue=True sinks inherited from the parent write synchronously in the child. On macOS, sinks added in the child do too. See Threads and Processes for the details.

Recap

  • logger.add("app.log", enqueue=True) writes in a background thread.
  • logger.complete() waits until all pending messages are written. Call it at shutdown and before reading your logs. Normal exits drain the queue on their own, killed processes don't.
  • Messages reach the file within ~100 ms. A sync sink with buffering=1 writes each line before the call returns.
  • The queue holds 10,000 messages, and waits instead of dropping when it's full.
  • enqueue=True is for not blocking, Logust's sync writes are already fast.