Skip to content

Computed properties - #406

Open
rwb27 wants to merge 10 commits into
mainfrom
computed-properties
Open

rwb27 wants to merge 10 commits into
mainfrom
computed-properties

Conversation

@rwb27

@rwb27 rwb27 commented Sep 14, 2026 •

Copy link
Copy Markdown
Collaborator

This is the start of computed properties. Computed properties are like functional properties, but they keep track of attribute access. This should then allow us to update them whenever their inputs change. This uses the same MessageBroker that handles passing notifications to websockets, so there's no extra code required in the properties we're watching.

Restrictions

Currently, an error is thrown if anything other than an observable property is accessed by the getter of a computed property. This is intended to reflect the limitations of the current computed property mechanism, but I think it's helpful behaviour. "manual computed properties" for now are just functional properties with the observable flag set to True. It may be possible in the future to mix computed and manual behaviour, but I don't want to make this PR any more confusing than it already is.

Concurrency

LabThings passes messages in the event loop, so we need to run a coroutine to observe our dependencies and recompute as required.

At the moment I think it's probably best to recompute the property in a worker thread, because it will cause confusion to run Thing code in the event loop. That might be a higher-performance alternative we can introduce in the future, but I will not do it now in the interest of doing one thing properly rather than several things confusingly.

We will need to signal the coroutine responsible for triggering recomputation to shut down when the server stops. Currently, that's tricky. This could use the same solution as the MJPEG stream in OpenFlexure. Might be a good time to finally merge that in to this repo.

There's a possible race condition in the current code: if a dependency is added during recomputation, and that dependency changes between the time when it's accessed and the end of the recomputation, the update might get missed. The solution to this is to have the recomputation (which runs in a worker thread) call back to the event loop and subscribe to properties as they are accessed. This isn't yet done.

Before merging:

This is the start of computed properties.
Computed properties are like functional properties, but they
keep track of attribute access.
This should then allow us to update them whenever their inputs change.

Currently, an error is thrown if anything other than an observable property is accessed.
@rwb27 rwb27 changed the title Functional properties that track attribute access Computed properties Sep 14, 2026
@barecheck

barecheck Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Barecheck - Code coverage report

Total: 97.51%

Your code coverage diff: 0.08% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/properties.py507, 525, 999, 1022
src/labthings_fastapi/server/__init__.py254, 263, 267, 331, 345-348

This will create streams and subscribe to updates on dependencies
whenever the property is read.

This should mean that we need only read the property once at the start, and  we'll then publish updates thereafter.

Currently there isn't any global trigger to start listening, and there's no way to shut down the listening coroutines.
The only missing feature is a way of manually triggering a recomputation, which might reintroduce much of the faff I eliminated.
This commit adds a hook to the lifecycle to listen for changes of
dependencies and recompute properties.
It also makes use of the message broker to close streams and shut
down said coroutines.
This adds an async test that checks we get a notification for a computed
property when its dependency changes.
This checks that changing dependencies are properly tracked.

Note that there's a lingering race condition because of the use of
`start_soon`.
This should probably be fixed by passing a task status handle to
the watching function.
I've eliminated the use of `start_soon` during set-up of computed properties.
This should eliminate a potential race condition, where the server starts up
before computed properties are fully operational.

I suspect this race condition is most likely to be a problem in the test
suite, but it's worth eliminating anyway.
This change pushes property subscription earlier in the process: we now make sure there's a
subscription to our dependencies before returning the dependency's current value.
This should eliminate the race condition where a property update
that occurs during recomputation is missed.

I've also tightened up the tests significantly, and considered recursion.

In tightening up the tests, I've modified the `publish` method to provide a way to trigger recomputation explicitly.
@rwb27
rwb27 requested a review from bprobert97 October 8, 2026 14:53
@rwb27
rwb27 marked this pull request as ready for review October 8, 2026 14:54

This branch has not been deployed

No deployments
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.

1 participant