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 is recursive;Falseotherwise.event_filter (Iterable[
watchdog.events.FileSystemEvent] | None) – Optional collection ofwatchdog.events.FileSystemEventto watch.follow_symlink (
bool) –Trueif symlinks are followed;Falseotherwise.
- property event_filter: frozenset[type[FileSystemEvent]] | None#
Collection of event types watched for the path
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: float = 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 (
float) – Timeout (in seconds) 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).
- 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: float) None[source]#
Override this method to populate the event queue with events per interval period.
- Parameters:
timeout (
float) – Timeout (in seconds) 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: float = 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 (
float) – Timeout value (in seconds) 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: float = 1.0)[source]#
Bases:
EventDispatcherBase observer.
- Parameters:
emitter_class (
type[EventEmitter]) – The class of the emitter to use (e.g., a subclass ofEventEmitter).timeout (
float) – The timeout (in seconds) for the observer to wait on events.
- 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 emitters: set[EventEmitter]#
Returns event emitter created by this observer.
- 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 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]#
Start the thread’s activity.
It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.
This method will raise a RuntimeError if called more than once on the same thread object.
- unschedule(watch: ObservedWatch) None[source]#
Unschedules a watch.
- Parameters:
watch (An instance of
ObservedWatchor a subclass ofObservedWatch) – The watch to unschedule.