watchdog.observers.api#

Immutables#

class watchdog.observers.api.ObservedWatch(path: str | Path, *, recursive: bool, event_filter: list[type[FileSystemEvent]] | None = None, follow_symlink: bool = False)[source]#

Bases: object

An scheduled watch.

Parameters:
property key: tuple[str, bool, frozenset[type[FileSystemEvent]] | None, bool]#

A tuple key identifying the watch (raw_path, recursive, event_filter, follow_symlink).

Collections#

class watchdog.observers.api.EventQueue(maxsize: int = 0)[source]#

Bases: SkipRepeatsQueue

Thread-safe event queue based on a special queue that skips adding the same event (watchdog.events.FileSystemEvent) multiple times consecutively. Thus avoiding dispatching multiple event handling calls when multiple identical events are produced quicker than an observer can consume them.

Parameters:

maxsize (int) – Maximum number of items allowed in the queue. If <= 0, the queue size is infinite.

Classes#

class watchdog.observers.api.EventEmitter(event_queue: EventQueue, watch: ObservedWatch, *, timeout: Seconds = 1.0, event_filter: list[type[FileSystemEvent]] | None = None)[source]#

Bases: BaseThread

Producer thread base class subclassed by event emitters that generate events and populate a queue with them.

Parameters:
  • event_queue (EventQueue) – The event queue to populate with generated events.

  • watch (ObservedWatch) – The watch to observe and produce events for.

  • timeout (Seconds) – Timeout between successive attempts at reading events.

  • event_filter (Iterable[watchdog.events.FileSystemEvent] | None) – Collection of event types to emit, or None for no filtering (default).

property exception: Exception | None#

The unhandled exception that terminated this emitter, if any.

flush() → None[source]#

Best-effort, backend-specific synchronization of pending native events into this emitter’s event queue.

When supported by the backend, events currently buffered by the native event source are processed and placed into the event queue before returning. This method does not dispatch events to handlers and does not guarantee that future filesystem changes have been observed by the operating system.

Does nothing by default on backends without explicit flush support (for example, polling emitters).

queue_event(event: FileSystemEvent) → None[source]#

Queues a single event.

Parameters:

event (An instance of watchdog.events.FileSystemEvent or a subclass.) – Event to be queued.

queue_events(timeout: Seconds) → None[source]#

Override this method to populate the event queue with events per interval period.

Parameters:

timeout (Seconds) – Timeout between successive attempts at reading events.

run() → None[source]#

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

property timeout: Seconds#

Blocking timeout for reading events.

property watch: ObservedWatch#

The watch associated with this emitter.

class watchdog.observers.api.EventDispatcher(*, timeout: Seconds = 1.0)[source]#

Bases: BaseThread

Consumer thread base class subclassed by event observer threads that dispatch events from an event queue to appropriate event handlers.

Parameters:

timeout (Seconds) – Timeout value passed to emitters constructions in the child class BaseObserver.

dispatch_events(event_queue: EventQueue) → None[source]#

Override this method to consume events from an event queue, blocking on the queue for the specified timeout before raising queue.Empty.

Parameters:

event_queue (EventQueue) – Event queue to populate with one set of events.

Raises:

queue.Empty

property event_queue: EventQueue#

The event queue which is populated with file system events by emitters and from which events are dispatched by a dispatcher thread.

run() → None[source]#

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

stop() → None[source]#

Signals the thread to stop.

stop_event = <object object>#

Event inserted into the queue to signal a requested stop.

property timeout: Seconds#

Timeout value to construct emitters with.

class watchdog.observers.api.BaseObserver(emitter_class: type[EventEmitter], *, timeout: Seconds = 1.0, **kwargs: Any)[source]#

Bases: EventDispatcher

Base observer.

Parameters:
  • emitter_class (type[EventEmitter]) – The class of the emitter to use (e.g., a subclass of EventEmitter).

  • timeout (Seconds) – The timeout for the observer to wait on events.

  • kwargs – Extra keyword arguments forwarded to every emitter this observer constructs, for platform-specific emitter options.

add_handler_for_watch(event_handler: FileSystemEventHandler, watch: ObservedWatch) → None[source]#

Adds a handler for the given watch.

Parameters:
  • event_handler (watchdog.events.FileSystemEventHandler or a subclass) – An event handler instance that has appropriate event handling methods which will be called by the observer in response to file system events.

  • watch (An instance of ObservedWatch or a subclass of ObservedWatch) – The watch to add a handler for.

dispatch_events(event_queue: EventQueue) → None[source]#

Override this method to consume events from an event queue, blocking on the queue for the specified timeout before raising queue.Empty.

Parameters:

event_queue (EventQueue) – Event queue to populate with one set of events.

Raises:

queue.Empty

property drain_completed: bool | None#

Whether the most recent stop(drain=True) confirmed all queued events were dispatched before shutdown.

Returns None if stop(drain=True) has not been called, True if the drain sentinel was processed by the dispatcher, or False if draining timed out, the dispatcher was not running, or stop(drain=True) was called from the dispatcher thread itself.

property emitters: set[EventEmitter]#

Returns event emitter created by this observer.

on_thread_start() → None[source]#

Start all scheduled emitters before the observer thread begins dispatching.

on_thread_stop() → None[source]#

Override this method instead of stop(). stop() calls this method.

This method is called immediately after the thread is signaled to stop.

remove_handler_for_watch(event_handler: FileSystemEventHandler, watch: ObservedWatch) → None[source]#

Removes a handler for the given watch.

Parameters:
  • event_handler (watchdog.events.FileSystemEventHandler or a subclass) – An event handler instance that has appropriate event handling methods which will be called by the observer in response to file system events.

  • watch (An instance of ObservedWatch or a subclass of ObservedWatch) – The watch to remove a handler for.

schedule(event_handler: FileSystemEventHandler, path: str | Path, *, recursive: bool = False, event_filter: list[type[FileSystemEvent]] | None = None, follow_symlink: bool = False) → ObservedWatch[source]#

Schedules watching a path and calls appropriate methods specified in the given event handler in response to file system events.

Parameters:
  • event_handler (watchdog.events.FileSystemEventHandler or a subclass) – An event handler instance that has appropriate event handling methods which will be called by the observer in response to file system events.

  • path (str or pathlib.Path) – Directory or file path that will be monitored.

  • recursive (bool) – True if events will be emitted for sub-directories traversed recursively; False otherwise.

  • event_filter (Iterable[watchdog.events.FileSystemEvent] | None) – Collection of event types to emit, or None for no filtering (default).

  • follow_symlink (bool) – True if symlinks should be followed; False otherwise.

Returns:

An ObservedWatch object instance representing a watch.

start() → None[source]#

Starts the observer thread and all scheduled emitters under synchronization lock.

Overrides watchdog.utils.BaseThread.start() to guarantee atomicity between observer startup and concurrent schedule() invocations (prevents #1044). If the observer is already running, logs a warning and returns.

stop(*, drain: bool = False, drain_timeout: float | None = None) → None[source]#

Signals the observer to stop.

Draining must occur here rather than in on_thread_stop() because watchdog.utils.BaseThread.stop() sets _stopped_event first, which immediately ends the dispatcher loop.

Parameters:
  • drain – If True, stops and joins all active emitters first so no new OS events are produced, waits for already-queued events to be dispatched to registered handlers, and then clears handlers and stops the dispatcher thread. Defaults to False (immediate stop).

  • drain_timeout – Maximum time in seconds to wait for queued events to drain after emitters have stopped when drain=True (does not bound emitter shutdown/join time). None (the default) waits indefinitely with no deadline. Ignored when drain=False.

unschedule(watch: ObservedWatch) → None[source]#

Unschedules a watch.

Parameters:

watch (An instance of ObservedWatch or a subclass of ObservedWatch) – The watch to unschedule.

unschedule_all() → None[source]#

Unschedules all watches and detaches all associated event handlers.