periodic_flush
A background thread that flushes a telemetry handler on a fixed interval.
Both Datadog handlers buffer until a size or capacity trigger fires, or until something explicitly flushes them at the end of a task or run. An indexing pass that runs for hours produces events steadily but reaches neither trigger, so its whole run arrives at Datadog in one batch when the run ends. A periodic flush bounds how long an event can sit in a buffer.
Classes
PeriodicFlusher
class PeriodicFlusher( flush: Callable[[], None], interval_seconds: float, name: str, on_error: ErrorReporter,):Calls a handler's flush on a fixed interval until stopped.
The thread is a daemon, so a handler nobody closed can never hold the
interpreter open at exit, and it waits on an Event rather than sleeping,
so stop returns as soon as it is asked to rather than at the end of the
current interval.
Initialise the flusher.
Arguments
flush: The handler's flush method.interval_seconds: How long to wait between flushes. Zero or less disables the periodic flush, leaving the handler with only its size, capacity and explicit triggers.name: Thread name, to tell the logs flusher from the metrics one in a stack dump.on_error: Called with a message when a flush raises.
Variables
is_running : bool- Whether a flush thread is currently alive.
Methods
start
def start(self) ‑> None:Start flushing, unless disabled or already started.
stop
def stop(self, join: bool = True) ‑> None:Stop flushing, by default waiting for the thread to finish.
Safe to call when the flusher never started, and safe to call twice.
Arguments
join: Whether to wait for a flush already in flight. True for an orderly shutdown, which wants the points that flush has already taken out of the buffer. False for a caller that may hold the handler lock -logging.shutdowndoes, acrossclose(), and waiting there is waiting for a thread that cannot proceed until the caller returns.