Skip to main content

WatchmanReloader

This class provides a file reloader implementation that utilizes the Facebook Watchman service for efficient file system monitoring. It manages subscriptions to file changes across multiple project roots and directories, supporting both direct file tracking and glob patterns. The reloader integrates with the Watchman client to receive real-time notifications, ensuring high-performance updates for large-scale projects.

Attributes

AttributeTypeDescription
rootsdefaultdict = defaultdict(set)A mapping of directory paths to sets of watched files used to track the filesystem hierarchy.
processed_requestthreading.Event = threading.Event()A thread synchronization event used to signal that a request has finished and watches should be updated.
client_timeoutint = 5Maximum time in seconds to wait for a response from the Watchman service, controlled by the DJANGO_WATCHMAN_TIMEOUT environment variable.
clientpywatchman.clientThe pywatchman client instance used to communicate with the Watchman service for queries and subscriptions.

Constructor

Signature

def WatchmanReloader() - > null

Methods


client()

@classmethod
def client() - > pywatchman.client

Returns a cached pywatchman client instance configured with the specified timeout.

Returns

TypeDescription
pywatchman.clientThe active Watchman client used for issuing queries and subscriptions

watched_roots()

@classmethod
def watched_roots(
watched_files: list
) - > frozenset

Calculates the set of all unique directory roots that should be monitored based on watched files and system paths.

Parameters

NameTypeDescription
watched_fileslistA list of individual file paths currently being tracked

Returns

TypeDescription
frozensetA set of Path objects representing the directories to be watched

update_watches()

@classmethod
def update_watches()

Updates the active Watchman watches and subscriptions, verifying server availability if an error occurs during the update.


request_processed()

@classmethod
def request_processed(
**kwargs: any
)

Signals that a request has been processed, triggering a refresh of watches on the next tick.

Parameters

NameTypeDescription
**kwargsanyArbitrary keyword arguments passed from the request_finished signal

tick()

@classmethod
def tick() - > generator

The main loop of the reloader that listens for Watchman events and updates watches when requests are processed.

Returns

TypeDescription
generatorA generator that yields control back to the caller between polling intervals

stop()

@classmethod
def stop()

Closes the Watchman client connection and stops the reloader.


check_server_status()

@classmethod
def check_server_status(
inner_ex: Exception
) - > boolean

Return True if the server is available.

Parameters

NameTypeDescription
inner_exExceptionAn optional exception to wrap if the server is unavailable

Returns

TypeDescription
booleanTrue if the Watchman server responds to a version query

check_availability()

@classmethod
def check_availability()

Verifies that the pywatchman library is installed and that the Watchman service is running with a compatible version.