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
| Attribute | Type | Description |
|---|---|---|
| roots | defaultdict = defaultdict(set) | A mapping of directory paths to sets of watched files used to track the filesystem hierarchy. |
| processed_request | threading.Event = threading.Event() | A thread synchronization event used to signal that a request has finished and watches should be updated. |
| client_timeout | int = 5 | Maximum time in seconds to wait for a response from the Watchman service, controlled by the DJANGO_WATCHMAN_TIMEOUT environment variable. |
| client | pywatchman.client | The 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
| Type | Description |
|---|---|
pywatchman.client | The 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
| Name | Type | Description |
|---|---|---|
| watched_files | list | A list of individual file paths currently being tracked |
Returns
| Type | Description |
|---|---|
frozenset | A 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
| Name | Type | Description |
|---|---|---|
| **kwargs | any | Arbitrary 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
| Type | Description |
|---|---|
generator | A 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
| Name | Type | Description |
|---|---|---|
| inner_ex | Exception | An optional exception to wrap if the server is unavailable |
Returns
| Type | Description |
|---|---|
boolean | True 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.