@@ -81,6 +81,12 @@ def watch(
81
81
* otherwise, force polling is enabled
82
82
* otherwise, we enable force polling only if we detect we're running on WSL (Windows Subsystem for Linux)
83
83
84
+ It is also possible to change the poll delay between iterations, it can be changed to maintain a good response time
85
+ and an appropiate CPU consumption using the `poll_delay_ms` argument, we change poll delay thus:
86
+
87
+ * if file polling is enabled and the `WATCHFILES_POLL_DELAY_MS` env var exists and it is numeric, we use that
88
+ * otherwise, we use the argument value
89
+
84
90
Args:
85
91
*paths: filesystem paths to watch.
86
92
watch_filter: callable used to filter out changes which are not important, you can either use a raw callable
@@ -114,6 +120,7 @@ def watch(
114
120
```
115
121
"""
116
122
force_polling = _default_force_polling (force_polling )
123
+ poll_delay_ms = _default_poll_delay_ms (poll_delay_ms )
117
124
ignore_permission_denied = _default_ignore_permission_denied (ignore_permission_denied )
118
125
debug = _default_debug (debug )
119
126
with RustNotify (
@@ -184,6 +191,7 @@ async def awatch( # C901
184
191
force_polling: if true, always use polling instead of file system notifications, default is `None` where
185
192
`force_polling` is set to `True` if the `WATCHFILES_FORCE_POLLING` environment variable exists.
186
193
poll_delay_ms: delay between polling for changes, only used if `force_polling=True`.
194
+ `poll_delay_ms` can be changed via the `WATCHFILES_POLL_DELAY_MS` environment variable.
187
195
recursive: if `True`, watch for changes in sub-directories recursively, otherwise watch only for changes in the
188
196
top-level directory, default is `True`.
189
197
ignore_permission_denied: if `True`, will ignore permission denied errors, otherwise will raise them by default.
@@ -242,6 +250,7 @@ async def stop_soon():
242
250
stop_event_ = stop_event
243
251
244
252
force_polling = _default_force_polling (force_polling )
253
+ poll_delay_ms = _default_poll_delay_ms (poll_delay_ms )
245
254
ignore_permission_denied = _default_ignore_permission_denied (ignore_permission_denied )
246
255
debug = _default_debug (debug )
247
256
with RustNotify (
@@ -327,6 +336,17 @@ def _default_force_polling(force_polling: Optional[bool]) -> bool:
327
336
return _auto_force_polling ()
328
337
329
338
339
+ def _default_poll_delay_ms (poll_delay_ms : int ) -> int :
340
+ """
341
+ See docstring for `watch` above for details.
342
+ """
343
+ env_var = os .getenv ('WATCHFILES_POLL_DELAY_MS' )
344
+ if env_var and env_var .isdecimal ():
345
+ return int (env_var )
346
+ else :
347
+ return poll_delay_ms
348
+
349
+
330
350
def _default_debug (debug : Optional [bool ]) -> bool :
331
351
if debug is not None :
332
352
return debug
0 commit comments