labthings_fastapi.dependencies.blocking_portal ============================================== .. py:module:: labthings_fastapi.dependencies.blocking_portal .. autoapi-nested-parse:: 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 :ref:`concurrency` 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 --------- .. autoapisummary:: labthings_fastapi.dependencies.blocking_portal.blocking_portal_from_thing_server Module Contents --------------- .. py:function:: 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 :mod:`.blocking_portal`. :param request: The `fastapi.Request` object, supplied by the :ref:`dependencies` mechanism. :return: the `anyio.from_thread.BlockingPortal` allowing access to the `.ThingServer`\ 's event loop.