Skip to content

Commit e14de63

Browse files
allow modify poll_delay_ms argument using an environment variable (#303)
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
1 parent e493e75 commit e14de63

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

tests/test_force_polling.py

+11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ def test_watch_polling_env(mocker, env: SetEnv):
4242
m.assert_called_once_with(['.'], False, True, 300, True, False)
4343

4444

45+
def test_watch_polling_env_with_custom_delay(mocker, env: SetEnv):
46+
env('WATCHFILES_FORCE_POLLING', '1')
47+
env('WATCHFILES_POLL_DELAY_MS', '1000')
48+
m = mocker.patch('watchfiles.main.RustNotify', return_value=MockRustNotify())
49+
50+
for _ in watch('.'):
51+
pass
52+
53+
m.assert_called_once_with(['.'], False, True, 1000, True, False)
54+
55+
4556
@pytest.mark.parametrize(
4657
'env_var,arg,expected',
4758
[

watchfiles/main.py

+20
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ def watch(
8181
* otherwise, force polling is enabled
8282
* otherwise, we enable force polling only if we detect we're running on WSL (Windows Subsystem for Linux)
8383
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+
8490
Args:
8591
*paths: filesystem paths to watch.
8692
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(
114120
```
115121
"""
116122
force_polling = _default_force_polling(force_polling)
123+
poll_delay_ms = _default_poll_delay_ms(poll_delay_ms)
117124
ignore_permission_denied = _default_ignore_permission_denied(ignore_permission_denied)
118125
debug = _default_debug(debug)
119126
with RustNotify(
@@ -184,6 +191,7 @@ async def awatch( # C901
184191
force_polling: if true, always use polling instead of file system notifications, default is `None` where
185192
`force_polling` is set to `True` if the `WATCHFILES_FORCE_POLLING` environment variable exists.
186193
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.
187195
recursive: if `True`, watch for changes in sub-directories recursively, otherwise watch only for changes in the
188196
top-level directory, default is `True`.
189197
ignore_permission_denied: if `True`, will ignore permission denied errors, otherwise will raise them by default.
@@ -242,6 +250,7 @@ async def stop_soon():
242250
stop_event_ = stop_event
243251

244252
force_polling = _default_force_polling(force_polling)
253+
poll_delay_ms = _default_poll_delay_ms(poll_delay_ms)
245254
ignore_permission_denied = _default_ignore_permission_denied(ignore_permission_denied)
246255
debug = _default_debug(debug)
247256
with RustNotify(
@@ -327,6 +336,17 @@ def _default_force_polling(force_polling: Optional[bool]) -> bool:
327336
return _auto_force_polling()
328337

329338

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+
330350
def _default_debug(debug: Optional[bool]) -> bool:
331351
if debug is not None:
332352
return debug

0 commit comments

Comments
 (0)