labthings_fastapi.dependencies.blocking_portal
FastAPI dependency for a blocking portal.
This allows dependencies that are called by threaded code to send things back to the async event loop. See Concurrency in LabThings-FastAPI for more details.
Threaded code can call asynchronous code in the anyio event loop used by
fastapi, if an anyio.BlockingPortal is used.
The ThingServer sets up an anyio.from_thread.BlockingPortal when the server starts
(in ThingServer.lifespan). This may be accessed from an action using the
BlockingPortal dependency in this module.
Note
The blocking portal is accessed via a dependency to ensure we only ever use the blocking portal attached to the server handling the current request.
This may be simplified in the future, as a Thing can only ever be
attached to one ThingServer, and each ThingServer corresponds
to exactly one event loop. That means a mechanism may be introduced in
the future that allows Thing code to access a blocking portal without
the need for a dependency.
Functions
Return the blocking portal from our ThingServer. |
Module Contents
- labthings_fastapi.dependencies.blocking_portal.blocking_portal_from_thing_server(request: fastapi.Request) anyio.from_thread.BlockingPortal
Return the blocking portal from our ThingServer.
This is for use as a FastAPI dependency, to allow threaded code to call async code. See the module-level docstring for
blocking_portal.- Parameters:
request – The
fastapi.Requestobject, supplied by the Dependencies mechanism.- Returns:
the
anyio.from_thread.BlockingPortalallowing access to theThingServer‘s event loop.