You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge pull request #194 from labthings/drop-dependencies-feedback
Drop dependencies
This PR replaces dependencies with several new features: `lt.thing_slot`, a `ThingServerInterface`, and `lt.cancellable_sleep`. These are all described in the documentation in more detail.
Dependencies and DirectThingClient are still in the codebase: these should be deprecated for the next release and removed in the following one.
Copy file name to clipboardExpand all lines: docs/source/concurrency.rst
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ In the case of properties, the HTTP response is only returned once the `.Thing`
11
11
12
12
Many of the functions that handle HTTP requests are asynchronous, running in an :mod:`anyio` event loop. This enables many HTTP connections to be handled at once with good efficiency. The `anyio documentation`_ describes the functions that link between async and threaded code. When the LabThings server is started, we create an :class:`anyio.from_thread.BlockingPortal`, which allows threaded code to run code asynchronously in the event loop.
13
13
14
-
An action can obtain the blocking portal using the `~labthings_fastapi.dependencies.blocking_portal.BlockingPortal` dependency, i.e. by declaring an argument of that type. This avoids referring to the blocking portal through a global variable, which could lead to confusion if there are multiple event loops, e.g. during testing.
14
+
An action can run async code using its server interface. See `.ThingServerInterface.start_async_task_soon` for details.
15
15
16
16
There are relatively few occasions when `.Thing` code will need to consider this explicitly: more usually the blocking portal will be obtained by a LabThings function, for example the `.MJPEGStream` class.
17
17
@@ -22,3 +22,11 @@ Calling Things from other Things
22
22
23
23
When one `Thing` calls the actions or properties of another `.Thing`, either directly or via a `.DirectThingClient`, no new threads are spawned: the action or property is run in the same thread as the caller. This mirrors the behaviour of the `.ThingClient`, which blocks until the action or property is complete. See :doc:`using_things` for more details on how to call actions and properties of other Things.
24
24
25
+
Invocations and concurrency
26
+
---------------------------
27
+
28
+
Each time an action is run ("invoked" in :ref:`wot_cc`), we create a new thread to run it. This thread has a context variable set, such that ``lt.cancellable_sleep`` and ``lt.get_invocation_logger`` are aware of which invocation is currently running. If an action spawns a new thread (e.g. using `threading.Thread`\ ), this new thread will not have an invocation ID, and consequently the two invocation-specific functions mentioned will not work.
29
+
30
+
Usually, the best solution to this problem is to generate a new invocation ID for the thread. This means only the original action thread will receive cancellation events, and only the original action thread will log to the invocation logger. If the action is cancelled, you must cancel the background thread. This is the behaviour of `.ThreadWithInvocationID`\ .
31
+
32
+
It is also possible to copy the current invocation ID to a new thread. This is often a bad idea, as it's ill-defined whether the exception will arise in the original thread or the new one if the invocation is cancelled. Logs from the two threads will also be interleaved. If it's desirable to log from the background thread, the invocation logger may safely be passed as an argument, rather than accessed via ``lt.get_invocation_logger``\ .
Copy file name to clipboardExpand all lines: docs/source/dependencies/dependencies.rst
+8Lines changed: 8 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,19 @@
3
3
Dependencies
4
4
============
5
5
6
+
.. warning::
7
+
8
+
The use of dependencies is now deprecated. See :ref:`thing_slots` and `.ThingServerInterface` for a more intuitive way to access that functionality.
9
+
6
10
LabThings makes use of the powerful "dependency injection" mechanism in FastAPI. You can see the `FastAPI documentation`_ for more information. In brief, FastAPI dependencies are annotated types that instruct FastAPI to supply certain function arguments automatically. This removes the need to set up resources at the start of a function, and ensures everything the function needs is declared and typed clearly. The most common use for dependencies in LabThings is where an action needs to make use of another `.Thing` on the same `.ThingServer`.
7
11
8
12
Inter-Thing dependencies
9
13
------------------------
10
14
15
+
.. warning::
16
+
17
+
These dependencies are deprecated - see :ref:`thing_slots` instead.
18
+
11
19
Simple actions depend only on their input parameters and the `.Thing` on which they are defined. However, it's quite common to need something else, for example accessing another `.Thing` instance on the same LabThings server. There are two important principles to bear in mind here:
12
20
13
21
* Other `.Thing` instances should be accessed using a `.DirectThingClient` subclass if possible. This creates a wrapper object that should work like a `.ThingClient`, meaning your code should work either on the server or in a client script. This makes the code much easier to debug.
Copy file name to clipboardExpand all lines: docs/source/index.rst
+15-19Lines changed: 15 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,11 @@ Documentation for LabThings-FastAPI
7
7
8
8
quickstart/quickstart.rst
9
9
wot_core_concepts.rst
10
-
lt_core_concepts.rst
10
+
structure.rst
11
11
tutorial/index.rst
12
12
examples.rst
13
13
actions.rst
14
+
thing_slots.rst
14
15
dependencies/dependencies.rst
15
16
blobs.rst
16
17
concurrency.rst
@@ -19,26 +20,21 @@ Documentation for LabThings-FastAPI
19
20
20
21
autoapi/index
21
22
22
-
`labthings-fastapi` implements a Web of Things interface for laboratory hardware using Python. This is a ground-up rewrite of python-labthings_, replacing Flask 1 and Marshmallow with FastAPI and Pydantic. It is the underlying framework for v3 of the `OpenFlexure Microscope software <https://gitlab.com/openflexure/openflexure-microscope-server/>`_.
23
+
`labthings-fastapi` is a Python library to simplify the process of making laboratory instruments available via a HTTP. It aims to create an API that is usable from any modern programming language, with API documentation in both :ref:`openapi` and :ref:`gen_td` formats. It is the underlying framework for v3 of the `OpenFlexure Microscope software <https://gitlab.com/openflexure/openflexure-microscope-server/>`_. Key features and design aims are:
23
24
24
-
`labthings-fastapi` aims to simplify the process of making laboratory instruments available via an HTTP API. Key features and design aims are below:
25
-
26
-
* Functionality together in `Thing` subclasses, which represent units of hardware or software (see :doc:`wot_core_concepts`)
27
-
* Methods and properties of `Thing` subclasses may be added to the HTTP API and Thing Description using decorators
25
+
* The functionality of a unit of hardware or software is described using `.Thing` subclasses.
26
+
* Methods and properties of `.Thing` subclasses may be added to the HTTP API and associated documentation using decorators.
27
+
* Datatypes of action input/outputs and properties are defined with Python type hints.
28
+
* Actions are decorated methods of a `.Thing` class. There is no need for separate schemas or endpoint definitions.
29
+
* Properties are defined either as typed attributes (similar to `pydantic` or `dataclasses`) or with a `property`\ -like decorator.
30
+
* Lifecycle and concurrency are appropriate for hardware: `Thing` code is always run in a thread, and each `Thing` is instantiated, started up, and shut down only once.
28
31
* Vocabulary and concepts are aligned with the `W3C Web of Things <https://www.w3.org/WoT/>`_ standard (see :doc:`wot_core_concepts`)
29
-
- Things are classes, with properties and actions defined exactly once
30
-
- Thing Descriptions are automatically generated, and validated with `pydantic`
31
-
- OpenAPI documentation is automatically generated by FastAPI
32
-
* We follow FastAPI_'s lead and try to use standard Python features to minimise unnecessary code
33
-
- Datatypes of action input/outputs and properties are defined with Python type hints
34
-
- Actions are defined exactly once, as a method of a `Thing` class
35
-
- Properties and actions are declared using decorators (or descriptors if that's preferred)
36
-
- FastAPI_ "Dependency injection" is used to manage relationships between Things and dependency on the server
37
-
* Lifecycle and concurrency are appropriate for hardware: `Thing` code is always run in a thread, and each `Thing` is instantiated and shut down only once.
38
-
- Starlette (used by FastAPI) can handle requests asynchronously - this improves performance and enables websockets and other long-lived connections.
39
-
- `Thing` code is still, for now, threaded. In the future it may become possible to us other concurrency models in `Thing` code.
40
-
41
-
Compared to `python-labthings`_, this framework updates dependencies, shrinks the codebase, and simplifies the API (see :doc:`lt_core_concepts`).
32
+
33
+
Previous version
34
+
----------------
35
+
36
+
This is a ground-up rewrite of python-labthings_, replacing Flask 1 and Marshmallow with FastAPI and Pydantic.
37
+
Compared to `python-labthings`_, this framework updates dependencies, shrinks the codebase, and simplifies the API (see :doc:`lt_structure`).
42
38
* FastAPI more or less completely eliminates OpenAPI generation code from our codebase
43
39
* Marshmallow schemas and endpoint classes are replaced with Python type hints, eliminating double- or triple-definition of actions and their inputs/outputs.
44
40
* Thing Description generation is very much simplified by the new structure (multiple Things instead of one massive Thing with many extensions)
0 commit comments