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:
objectAn scheduled watch.
- Parameters:
path (
strorpathlib.Path) – Path string.recursive (
bool) –Trueif watch should include subdirectories;Falseotherwise.event_filter (Iterable[
watchdog.events.FileSystemEvent] | None) – Optional collection ofwatchdog.events.FileSystemEventto watch.follow_symlink (
bool) –Trueif symlinks are followed;Falseotherwise.
Collections#
- class watchdog.observers.api.EventQueue(maxsize: int = 0)[source]#
Bases:
SkipRepeatsQueueThread-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:
BaseThreadProducer 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).
- 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.FileSystemEventor 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 watch: ObservedWatch#
The watch associated with this emitter.
- class watchdog.observers.api.EventDispatcher(*, timeout: Seconds = 1.0)[source]#
Bases:
BaseThreadConsumer 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:
- 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_event = <object object>#
Event inserted into the queue to signal a requested stop.
- class watchdog.observers.api.BaseObserver(emitter_class: type[EventEmitter], *, timeout: Seconds = 1.0, **kwargs: Any)[source]#
Bases:
EventDispatcherBase observer.
- Parameters:
emitter_class (
type[EventEmitter]) – The class of the emitter to use (e.g., a subclass ofEventEmitter).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.FileSystemEventHandleror 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
ObservedWatchor a subclass ofObservedWatch) – 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:
- property drain_completed: bool | None#
Whether the most recent
stop(drain=True)confirmed all queued events were dispatched before shutdown.Returns
Noneifstop(drain=True)has not been called,Trueif the drain sentinel was processed by the dispatcher, orFalseif draining timed out, the dispatcher was not running, orstop(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.FileSystemEventHandleror 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
ObservedWatchor a subclass ofObservedWatch) – 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.FileSystemEventHandleror 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 (
strorpathlib.Path) – Directory or file path that will be monitored.recursive (
bool) –Trueif events will be emitted for sub-directories traversed recursively;Falseotherwise.event_filter (Iterable[
watchdog.events.FileSystemEvent] | None) – Collection of event types to emit, or None for no filtering (default).follow_symlink (
bool) –Trueif symlinks should be followed;Falseotherwise.
- Returns:
An
ObservedWatchobject 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 concurrentschedule()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()becausewatchdog.utils.BaseThread.stop()sets_stopped_eventfirst, 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 toFalse(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 whendrain=False.
- unschedule(watch: ObservedWatch) None[source]#
Unschedules a watch.
- Parameters:
watch (An instance of
ObservedWatchor a subclass ofObservedWatch) – The watch to unschedule.