Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor(watcher): replace Queue with Semaphore #1118

Merged
merged 6 commits into from Apr 15, 2023
Merged

Conversation

dairiki
Copy link
Contributor

@dairiki dairiki commented Mar 10, 2023

The current implementation stores all unprocessed filesystem change events in an unbounded Queue. There can possibly be quite a number of events, so this can be (in some situations) a memory consumption issue.

(In addition, we were storing, event-type and path in the queue, though we never referenced it.)

All we really care about is whether there has been any interesting event since the last time we checked. All we need for that is a flag, not a queue. So here we replace the queue.Queue with a threading.Event threading.BoundedSemaphore.

API Changes

This does change the API to our Watcher class. I think Lektor itself is the only user of that, however, so I don't think that's a big deal.

We might as well remove the lektor.watcher.watch function, too. It's just as easy to use the Watcher class directly.

Other Changes

There's logic in the current code that, for successful builds, only check for file changes that occur after the end of the last successful build. (For failed builds, this logic is bypassed, so any unprocessed changes will trigger a new build-all.)
I think both of these are not quite correct. Really we want to check for any changes since the beginning of the previous build — whether or not it was successful.

Issue(s) Resolved

Fixes #

Related Issues / Links

Depends on #1117

Description of Changes

  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)

@dairiki dairiki force-pushed the fix.watcher-fileopened-events branch from 1d790bb to 450b56b Compare March 10, 2023 19:16
@dairiki dairiki changed the title Refactor(watcher): replace Queue with flag (threading.Event) Refactor(watcher): replace Queue with Semaphore Mar 12, 2023
@dairiki dairiki force-pushed the fix.watcher-fileopened-events branch from 450b56b to 474fd20 Compare March 12, 2023 16:36
@dairiki dairiki force-pushed the refactor.watcher branch 2 times, most recently from 5084991 to 63e35b7 Compare March 13, 2023 03:30
@dairiki dairiki force-pushed the fix.watcher-fileopened-events branch 2 times, most recently from 2276354 to db85427 Compare March 13, 2023 03:36
@dairiki dairiki force-pushed the fix.watcher-fileopened-events branch from db85427 to 9ced7ef Compare March 13, 2023 03:50
@dairiki dairiki force-pushed the refactor.watcher branch 2 times, most recently from 960e24e to 1e8c7ee Compare March 13, 2023 21:35
@dairiki dairiki added this to the 3.4 milestone Mar 14, 2023
@dairiki dairiki marked this pull request as ready for review April 2, 2023 23:21
The current implementation stores all unprocessed filesystem change
events in an unbounded Queue.  There can possibly be quite a number of
events, so this can be (in some situations) a memory consumption
issue.

All we really care is whether there has been *any* interesting event
since the last time we checked. All we need for that is a flag, not a
queue.
This avoid a tiny (unlikely) race condition with the Event
strategy.
Remove the unused `time` and `event_type` parameters from
`Watcher.is_interesting()`.
@dairiki dairiki changed the base branch from fix.watcher-fileopened-events to master April 12, 2023 21:18
@dairiki dairiki merged commit 9f7261d into master Apr 15, 2023
15 checks passed
@yagebu yagebu deleted the refactor.watcher branch April 15, 2023 07:19
dairiki added a commit to dairiki/lektor that referenced this pull request Sep 11, 2023
* refactor(watcher): replace Queue with flag (threading.BoundedSemaphore)

The current implementation stores all unprocessed filesystem change
events in an unbounded Queue.  There can possibly be quite a number of
events, so this can be (in some situations) a memory consumption
issue.

All we really care is whether there has been *any* interesting event
since the last time we checked. All we need for that is a flag, not a
queue.

* refactor(watcher): skip is_interesting check if event already set

* refactor(watcher): remove unused params from is_interesting

Remove the unused `time` and `event_type` parameters from
`Watcher.is_interesting()`.

* refactor(watcher): delete lektor.watcher.watch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant