Skip to content

Startup ordering - #397

Open
rwb27 wants to merge 11 commits into
mainfrom
startup-ordering
Open

rwb27 wants to merge 11 commits into
mainfrom
startup-ordering

Conversation

@rwb27

@rwb27 rwb27 commented Aug 6, 2026 •

Copy link
Copy Markdown
Collaborator

This PR allows Things to specify that some Thing Slots must be started first. That means the connected Thing will already be running when __enter__ is called on the host Thing. This should make it easier to write code that interacts with other Things on start-up.

The new syntax looks like the example in #388:

import labthings_fastapi as lt
from .other_thing import MyOtherThing

class MyThing(lt.Thing):
    wrapped: MyOtherThing = lt.thing_slot(start_before=True)

    def __enter__(self) -> None:
        # LabThings will have ensured that `self.wrapped` started up before
        # this Thing starts up, i.e. before `__enter__` is called.
        self.wrapped.do_something()

Without start_before=True there is no guarantee that self.wrapped has started up, so the function call in __enter__ may or may not work.

Things to do before merge:

  • Add example code here rather than in Ordered Initialisation of Thing Slots #388
  • Consider deferred=False instead of start_first=True to enable this behaviour.
  • Add example code in documentation
  • Tidy up tests with a common base class
  • Make sure dependencies on all things/the current thing get ignored (because that's always circular)

Closes #388

@rwb27
rwb27 marked this pull request as ready for review November 2, 2026 23:30
@barecheck

This comment was marked as outdated.

@rwb27
rwb27 force-pushed the startup-ordering branch 2 times, most recently from 374df1a to 7206548 Compare November 5, 2026 23:20
@rwb27
rwb27 requested a review from bprobert97 November 6, 2026 07:46

@Dogmans Dogmans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation in the loop makes sense, only suggestion is maybe require_started=True rather than deferred possibly.

@rwb27

rwb27 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

Implementation in the loop makes sense, only suggestion is maybe require_started=True rather than deferred possibly.

Thanks - I've wavered a bit on what to call it: naming things is hard. I previously called it start_first but I then confused myself into wondering whether that meant "start the host thing first" or "start the connected thing first". I think require_started might be similarly confusing: my guess from that name would be that if I use the slot and the Thing hasn't started yet, I'll get an error (which isn't true).

Thinking about it, maybe required_for_startup would be least ambiguous - but also quite verbose. My rationale in picking deferred was that it felt like it meant the slot was able to load after the host. It also felt like it would be a good idea to check what "deferred" meant in the docs, whereas the various start-containing arguments I'd be tempted to make assumptions about...

As you can see, I've thought about it and I've not come to a strong conclusion...

Comment thread src/labthings_fastapi/thing_slots.py Resolved
Comment thread src/labthings_fastapi/thing_slots.py Resolved
Comment thread src/labthings_fastapi/thing_slots.py Resolved
Comment thread src/labthings_fastapi/thing_slots.py Resolved
Comment thread tests/test_thing_slots.py Resolved
Comment thread tests/test_thing_slots.py Resolved
Comment thread tests/test_thing_slots.py Resolved
Comment thread tests/test_server.py Resolved
rwb27 added 10 commits November 24, 2026 15:07
Also, `attributes` is never used. I've merged it with `class_attributes` to reduce pointless duplication.
This commit adds a `start_first` attribute to `lt.thing_slot` and a simple algorithm that
determines the order in which things must be started in order to fulfil those constraints.

It's not guaranteed to work, and will give a helpful error if that happens.
This should check that Things get ordered correctly.
Non-deferred Thing Slots now ignore dependencies on the host Thing.
This is most likely to happen with a Thing Slot typed as `lt.Thing` which matches all
Things on a server.

I've added a test for this.
This gets mypy passing again (some failures appeared when I rebased
due to a mypy upgrade).
This adds tests that cover:
* the startup order not matching the server's `things`.
* every case of `_connected_thing_names`, including no thing connected.
This implements @bprobert97's suggestion to preserve the order of the dictionary keys if there are no other constraints.
This mostly involved swapping some sets for lists.

I realised that preserving the dict order really helps with the tests. I can repeat
tests with the dict keys in different orders, and check that the order is preserved
when there are no constraints, but fixed when there's a constraint.
The argument used to affect start up order was previously `start_first`, then `deferred`.
I'm going to revert to `start_before` as it is less confusing.

This commit updates the code and docs to reflect that. I've checked the docs and
done some searches to try to catch the inconsistencies.
@github-actions

Copy link
Copy Markdown
100.0%

 passed

████████████████████ 100.0% on changed lines

55 lines changed   0 uncovered   4 files

 📂 4 files changed
  File Coverage Uncovered Lines
🟢 src/labthings_fastapi/thing_slots.py 100.0% —

| 🟢 | src/labthings_fastapi/exceptions.py | 100.0% | — |

| 🟢 | src/labthings_fastapi/utilities/__init__.py | 100.0% | — |

| 🟢 | src/labthings_fastapi/server/__init__.py | 100.0% | — |

 📋 Full diff-cover report

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • src/labthings_fastapi/exceptions.py (100%)
  • src/labthings_fastapi/server/init.py (100%)
  • src/labthings_fastapi/thing_slots.py (100%)
  • src/labthings_fastapi/utilities/init.py (100%)

Summary

  • Total: 55 lines
  • Missing: 0 lines
  • Coverage: 100%

🛡️ diff-cover-action

@rwb27

rwb27 commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

I've changed the argument to start_before and made the behaviour more deterministic: that addresses all the review comments so I hope this is ready for sign-off. Preserving the dict order makes the test better, as we can cycle through several orders when there are no constraints.

@rwb27
rwb27 requested a review from bprobert97 November 26, 2026 10:51
in which things will be started: it's assumed the slot will not be
accessed until all Things have started.

If `start_before` is set to `False` then LabThings

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If `start_before` is set to `False` then LabThings
If `start_before` is set to `True` then LabThings

in which things will be started: it's assumed the slot will not be
accessed until all Things have started.

If `start_before` is set to `False` then LabThings

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If `start_before` is set to `False` then LabThings
If `start_before` is set to `True` then LabThings

Comment thread tests/test_thing_slots.py


def test_start_before_default():
"""Check that the default value of `start_before` is True."""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Check that the default value of `start_before` is True."""
"""Check that the default value of `start_before` is False."""

Comment thread tests/test_thing_slots.py
({"b": ThingThree, "a": ThingWithoutDependency}, {("b", "a")}),
# If start_before==True, the list order can take only one value
({"a": ThingWithDependency, "b": ThingThree}, {("b", "a")}),
({"b": ThingThree, "a": ThingWithoutDependency}, {("b", "a")}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
({"b": ThingThree, "a": ThingWithoutDependency}, {("b", "a")}),
({"b": ThingThree, "a": ThingWithDependency}, {("b", "a")}),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

({"b": ThingThree, "a": ThingWithoutDependency}, {("b", "a")}) is already a test case on line 564

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.

Ordered Initialisation of Thing Slots

3 participants