From 9d5f9059e96a3cc4c4068caafa80b2b0fe456f32 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 17 Apr 2026 12:28:15 +0300 Subject: [PATCH 01/13] Add FastStream example to docs --- docs/examples/faststream.rst | 42 ++++++++++++++ .../miniapps/faststream/Dockerfile.consumer | 11 ++++ .../miniapps/faststream/Dockerfile.producer | 11 ++++ examples/miniapps/faststream/README.rst | 40 +++++++++++++ .../miniapps/faststream/docker-compose.yml | 22 +++++++ examples/miniapps/faststream/faststream.rst | 42 ++++++++++++++ examples/miniapps/faststream/requirements.txt | 4 ++ examples/miniapps/faststream/run.sh | 15 +++++ examples/miniapps/faststream/src/__init__.py | 0 examples/miniapps/faststream/src/consumer.py | 57 +++++++++++++++++++ examples/miniapps/faststream/src/producer.py | 25 ++++++++ 11 files changed, 269 insertions(+) create mode 100644 docs/examples/faststream.rst create mode 100644 examples/miniapps/faststream/Dockerfile.consumer create mode 100644 examples/miniapps/faststream/Dockerfile.producer create mode 100644 examples/miniapps/faststream/README.rst create mode 100644 examples/miniapps/faststream/docker-compose.yml create mode 100644 examples/miniapps/faststream/faststream.rst create mode 100644 examples/miniapps/faststream/requirements.txt create mode 100755 examples/miniapps/faststream/run.sh create mode 100644 examples/miniapps/faststream/src/__init__.py create mode 100644 examples/miniapps/faststream/src/consumer.py create mode 100644 examples/miniapps/faststream/src/producer.py diff --git a/docs/examples/faststream.rst b/docs/examples/faststream.rst new file mode 100644 index 000000000..9e267ea80 --- /dev/null +++ b/docs/examples/faststream.rst @@ -0,0 +1,42 @@ +.. _faststream-example: + +FastStream example +============= + +.. meta:: + :keywords: Python,Dependency Injection,FastStream,Example + :description: This example demonstrates a usage of FastStream with Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with `FastStream `_. + +The source code is available on the `Github `_. + +Despite ``FastStream`` uses ``FastDepends`` library for dependency injection, the integration between +``Dependency injector`` and ``FastStream`` has a small difference from already existing :ref:`fastdepends` example. + +Since ``FastStream`` also leverages function signatures to determine input data types you have to use ``Depends()`` function +with ``cast=False`` argument to make ``FastStream`` ignore your injected dependency argument in the function signature. + +Example below shows how to inject ``Counter`` class into ``FastStream`` redis handler so that it will distinguish between +message schema (``User``) and injected dependency (``Counter``) and use them both correctly. + +Listing of ``consumer.py``: + +.. literalinclude:: ../../examples/miniapps/faststream/src/consumer.py + :language: python + +Listing of ``producer.py``: + +.. literalinclude:: ../../examples/miniapps/faststream/src/producer.py + :language: python + +Sources +------- + +Explore the sources on the `Github `_. + +.. include:: ../sponsor.rst + +.. disqus:: + diff --git a/examples/miniapps/faststream/Dockerfile.consumer b/examples/miniapps/faststream/Dockerfile.consumer new file mode 100644 index 000000000..44d124c83 --- /dev/null +++ b/examples/miniapps/faststream/Dockerfile.consumer @@ -0,0 +1,11 @@ +FROM python:3.13-bookworm + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install -r requirements.txt + +COPY ./src ./src + +ENV PYTHONUNBUFFERED=1 +CMD ["python3", "-m", "src.consumer"] diff --git a/examples/miniapps/faststream/Dockerfile.producer b/examples/miniapps/faststream/Dockerfile.producer new file mode 100644 index 000000000..d36cd4e0b --- /dev/null +++ b/examples/miniapps/faststream/Dockerfile.producer @@ -0,0 +1,11 @@ +FROM python:3.13-bookworm + +WORKDIR /app + +COPY requirements.txt ./ +RUN pip install -r requirements.txt + +COPY ./src ./src + +ENV PYTHONUNBUFFERED=1 +CMD ["python3", "-m", "src.producer"] diff --git a/examples/miniapps/faststream/README.rst b/examples/miniapps/faststream/README.rst new file mode 100644 index 000000000..81279631e --- /dev/null +++ b/examples/miniapps/faststream/README.rst @@ -0,0 +1,40 @@ +FastStream + Dependency Injector Example +===================================== + +This is a `FastStream `_ + +`Dependency Injector `_ example application. + +The example application is a simple consumer that counts messages sent to redis channel by producer. + +Counter is provided to faststream handler as a dependency injected by ``dependency_injector`` library. + +Run +--- + +Everything can be run via docker compose. + +A convenient ``run.sh`` script runs consumer, producer and redis services, prints logs from consumer +and shuts down once producer exits. + +Ensure that ``run.sh`` has execution permission: + +.. code-block:: bash + + sudo chmod +x ./run.sh + +Run the sciprt: + +.. code-block:: bash + + ./run.sh + +The output should be something like: + +.. code-block:: + + faststream-example-consumer | Message #1 from John: 'As you can see' + faststream-example-consumer | Message #2 from John: 'messages are counted correctly' + faststream-example-consumer | Message #3 from John: 'by the counter that is injected' + faststream-example-consumer | Message #4 from John: 'into faststream handler' + faststream-example-consumer | Message #5 from John: 'via awesome dependency_injector library.' + diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml new file mode 100644 index 000000000..de980b14b --- /dev/null +++ b/examples/miniapps/faststream/docker-compose.yml @@ -0,0 +1,22 @@ +name: faststream-example + +services: + + redis: + container_name: "${COMPOSE_PROJECT_NAME}-redis" + image: redis + + consumer: + container_name: "${COMPOSE_PROJECT_NAME}-consumer" + build: + dockerfile: Dockerfile.consumer + depends_on: + - redis + + producer: + container_name: "${COMPOSE_PROJECT_NAME}-producer" + build: + dockerfile: Dockerfile.producer + depends_on: + - consumer + diff --git a/examples/miniapps/faststream/faststream.rst b/examples/miniapps/faststream/faststream.rst new file mode 100644 index 000000000..9e267ea80 --- /dev/null +++ b/examples/miniapps/faststream/faststream.rst @@ -0,0 +1,42 @@ +.. _faststream-example: + +FastStream example +============= + +.. meta:: + :keywords: Python,Dependency Injection,FastStream,Example + :description: This example demonstrates a usage of FastStream with Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with `FastStream `_. + +The source code is available on the `Github `_. + +Despite ``FastStream`` uses ``FastDepends`` library for dependency injection, the integration between +``Dependency injector`` and ``FastStream`` has a small difference from already existing :ref:`fastdepends` example. + +Since ``FastStream`` also leverages function signatures to determine input data types you have to use ``Depends()`` function +with ``cast=False`` argument to make ``FastStream`` ignore your injected dependency argument in the function signature. + +Example below shows how to inject ``Counter`` class into ``FastStream`` redis handler so that it will distinguish between +message schema (``User``) and injected dependency (``Counter``) and use them both correctly. + +Listing of ``consumer.py``: + +.. literalinclude:: ../../examples/miniapps/faststream/src/consumer.py + :language: python + +Listing of ``producer.py``: + +.. literalinclude:: ../../examples/miniapps/faststream/src/producer.py + :language: python + +Sources +------- + +Explore the sources on the `Github `_. + +.. include:: ../sponsor.rst + +.. disqus:: + diff --git a/examples/miniapps/faststream/requirements.txt b/examples/miniapps/faststream/requirements.txt new file mode 100644 index 000000000..8d9a0c605 --- /dev/null +++ b/examples/miniapps/faststream/requirements.txt @@ -0,0 +1,4 @@ +dependency_injector +faststream +pydantic +redis diff --git a/examples/miniapps/faststream/run.sh b/examples/miniapps/faststream/run.sh new file mode 100755 index 000000000..93bad7b7f --- /dev/null +++ b/examples/miniapps/faststream/run.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +docker compose up \ + --no-attach=redis \ + --abort-on-container-exit \ + --exit-code-from producer + +docker container rm \ + faststream-example-producer \ + faststream-example-consumer \ + faststream-example-redis + +docker image rm \ + faststream-example-producer \ + faststream-example-consumer diff --git a/examples/miniapps/faststream/src/__init__.py b/examples/miniapps/faststream/src/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examples/miniapps/faststream/src/consumer.py b/examples/miniapps/faststream/src/consumer.py new file mode 100644 index 000000000..fff66626b --- /dev/null +++ b/examples/miniapps/faststream/src/consumer.py @@ -0,0 +1,57 @@ +import asyncio +from typing import Annotated + +from dependency_injector import containers, providers +from dependency_injector.wiring import Provide, inject +from faststream import Depends, FastStream +from faststream.redis import RedisBroker +from pydantic import BaseModel + + +class Counter: + def __init__(self): + self.count = 0 + + def next(self) -> int: + self.count += 1 + return self.count + + +class Container(containers.DeclarativeContainer): + counter = providers.Singleton(Counter) + + +broker = RedisBroker("redis://redis", logger=None) + + +class Message(BaseModel): + user: str + text: str + + +@broker.subscriber("messages") +@inject +async def handle_user_message( + message: Message, + counter: Annotated[ + Counter, + Depends( + Provide[Container.counter], + cast=False, # <-- this is the key part + ), + ], +) -> None: + count = counter.next() + print(f"Message #{count} from {message.user}: '{message.text}'") + + +async def main() -> None: + container = Container() + container.wire(modules=[__name__]) + + await FastStream(broker, logger=None).run() + + +if __name__ == "__main__": + asyncio.run(main()) + diff --git a/examples/miniapps/faststream/src/producer.py b/examples/miniapps/faststream/src/producer.py new file mode 100644 index 000000000..d5c84ffe4 --- /dev/null +++ b/examples/miniapps/faststream/src/producer.py @@ -0,0 +1,25 @@ +import json +import time + +import redis + + +def main(): + client = redis.Redis(host="redis", port=6379) + + for text in ( + "As you can see", + "messages are counted correctly", + "by the counter that is injected", + "into faststream handler", + "via awesome dependency_injector library.", + ): + time.sleep(2) + + message = {"user": "John", "text": text} + client.publish("messages", json.dumps(message)) + + +if __name__ == "__main__": + main() + From af133cb71de4d6529a2a4692af6f5f2db872120f Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Thu, 23 Apr 2026 13:45:57 +0300 Subject: [PATCH 02/13] Add PR review fixes --- docs/examples/faststream.rst | 2 +- docs/examples/index.rst | 1 + .../faststream/{Dockerfile.consumer => Dockerfile} | 1 - examples/miniapps/faststream/Dockerfile.producer | 11 ----------- examples/miniapps/faststream/README.rst | 13 +++++++------ examples/miniapps/faststream/docker-compose.yml | 6 ++++-- examples/miniapps/faststream/run.sh | 8 -------- 7 files changed, 13 insertions(+), 29 deletions(-) rename examples/miniapps/faststream/{Dockerfile.consumer => Dockerfile} (79%) delete mode 100644 examples/miniapps/faststream/Dockerfile.producer diff --git a/docs/examples/faststream.rst b/docs/examples/faststream.rst index 9e267ea80..ae8a116ff 100644 --- a/docs/examples/faststream.rst +++ b/docs/examples/faststream.rst @@ -8,7 +8,7 @@ FastStream example :description: This example demonstrates a usage of FastStream with Dependency Injector. -This example shows how to use ``Dependency Injector`` with `FastStream `_. +This example shows how to use ``Dependency Injector`` with `FastStream `_. The source code is available on the `Github `_. diff --git a/docs/examples/index.rst b/docs/examples/index.rst index b166ceaeb..40f154702 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -23,5 +23,6 @@ Explore the examples to see the ``Dependency Injector`` in action. fastapi-redis fastapi-sqlalchemy fastdepends + faststream .. disqus:: diff --git a/examples/miniapps/faststream/Dockerfile.consumer b/examples/miniapps/faststream/Dockerfile similarity index 79% rename from examples/miniapps/faststream/Dockerfile.consumer rename to examples/miniapps/faststream/Dockerfile index 44d124c83..80854ecb8 100644 --- a/examples/miniapps/faststream/Dockerfile.consumer +++ b/examples/miniapps/faststream/Dockerfile @@ -8,4 +8,3 @@ RUN pip install -r requirements.txt COPY ./src ./src ENV PYTHONUNBUFFERED=1 -CMD ["python3", "-m", "src.consumer"] diff --git a/examples/miniapps/faststream/Dockerfile.producer b/examples/miniapps/faststream/Dockerfile.producer deleted file mode 100644 index d36cd4e0b..000000000 --- a/examples/miniapps/faststream/Dockerfile.producer +++ /dev/null @@ -1,11 +0,0 @@ -FROM python:3.13-bookworm - -WORKDIR /app - -COPY requirements.txt ./ -RUN pip install -r requirements.txt - -COPY ./src ./src - -ENV PYTHONUNBUFFERED=1 -CMD ["python3", "-m", "src.producer"] diff --git a/examples/miniapps/faststream/README.rst b/examples/miniapps/faststream/README.rst index 81279631e..51cd1c0d3 100644 --- a/examples/miniapps/faststream/README.rst +++ b/examples/miniapps/faststream/README.rst @@ -1,5 +1,5 @@ FastStream + Dependency Injector Example -===================================== +======================================== This is a `FastStream `_ + `Dependency Injector `_ example application. @@ -16,11 +16,6 @@ Everything can be run via docker compose. A convenient ``run.sh`` script runs consumer, producer and redis services, prints logs from consumer and shuts down once producer exits. -Ensure that ``run.sh`` has execution permission: - -.. code-block:: bash - - sudo chmod +x ./run.sh Run the sciprt: @@ -38,3 +33,9 @@ The output should be something like: faststream-example-consumer | Message #4 from John: 'into faststream handler' faststream-example-consumer | Message #5 from John: 'via awesome dependency_injector library.' + +Once you've done working with this example you can clean up docker images and containers it produced: + +.. code-block:: bash + + docker compose down --rmi local diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index de980b14b..f55388482 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -9,14 +9,16 @@ services: consumer: container_name: "${COMPOSE_PROJECT_NAME}-consumer" build: - dockerfile: Dockerfile.consumer + dockerfile: Dockerfile depends_on: - redis + entrypoint: python3 -m src.consumer producer: container_name: "${COMPOSE_PROJECT_NAME}-producer" build: - dockerfile: Dockerfile.producer + dockerfile: Dockerfile depends_on: - consumer + entrypoint: python3 -m src.producer diff --git a/examples/miniapps/faststream/run.sh b/examples/miniapps/faststream/run.sh index 93bad7b7f..f5fd00b99 100755 --- a/examples/miniapps/faststream/run.sh +++ b/examples/miniapps/faststream/run.sh @@ -5,11 +5,3 @@ docker compose up \ --abort-on-container-exit \ --exit-code-from producer -docker container rm \ - faststream-example-producer \ - faststream-example-consumer \ - faststream-example-redis - -docker image rm \ - faststream-example-producer \ - faststream-example-consumer From 0af3d3c55bf5fdbea52f221fa008d29d066c1b04 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:10:37 +0300 Subject: [PATCH 03/13] Add full DI for consumer.py --- .../miniapps/faststream/docker-compose.yml | 2 ++ examples/miniapps/faststream/src/consumer.py | 19 +++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index f55388482..0dfe89a26 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -10,6 +10,8 @@ services: container_name: "${COMPOSE_PROJECT_NAME}-consumer" build: dockerfile: Dockerfile + environment: + REDIS_URL: "redis://redis" depends_on: - redis entrypoint: python3 -m src.consumer diff --git a/examples/miniapps/faststream/src/consumer.py b/examples/miniapps/faststream/src/consumer.py index fff66626b..df5ddc631 100644 --- a/examples/miniapps/faststream/src/consumer.py +++ b/examples/miniapps/faststream/src/consumer.py @@ -4,7 +4,7 @@ from dependency_injector import containers, providers from dependency_injector.wiring import Provide, inject from faststream import Depends, FastStream -from faststream.redis import RedisBroker +from faststream.redis import RedisBroker, RedisRouter from pydantic import BaseModel @@ -20,8 +20,10 @@ def next(self) -> int: class Container(containers.DeclarativeContainer): counter = providers.Singleton(Counter) + config = providers.Configuration() -broker = RedisBroker("redis://redis", logger=None) + broker = providers.Singleton(RedisBroker, config.redis_url, logger=None) + app = providers.Factory(FastStream, broker, logger=None) class Message(BaseModel): @@ -29,7 +31,10 @@ class Message(BaseModel): text: str -@broker.subscriber("messages") +router = RedisRouter() + + +@router.subscriber("messages") @inject async def handle_user_message( message: Message, @@ -49,7 +54,13 @@ async def main() -> None: container = Container() container.wire(modules=[__name__]) - await FastStream(broker, logger=None).run() + container.config.redis_url.from_env("REDIS_URL") + + broker = container.broker() + broker.include_router(router) + + app = container.app() + await app.run() if __name__ == "__main__": From 1776d136932917c375499af4d589b84e1557f9c3 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:17:21 +0300 Subject: [PATCH 04/13] Add full DI to producer.py --- .../miniapps/faststream/docker-compose.yml | 3 +++ examples/miniapps/faststream/src/producer.py | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index 0dfe89a26..f6a246db2 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -20,6 +20,9 @@ services: container_name: "${COMPOSE_PROJECT_NAME}-producer" build: dockerfile: Dockerfile + environment: + REDIS_HOST: "redis" + REDIS_PORT: "6379" depends_on: - consumer entrypoint: python3 -m src.producer diff --git a/examples/miniapps/faststream/src/producer.py b/examples/miniapps/faststream/src/producer.py index d5c84ffe4..bd69f5ae2 100644 --- a/examples/miniapps/faststream/src/producer.py +++ b/examples/miniapps/faststream/src/producer.py @@ -1,11 +1,25 @@ import json import time -import redis +from dependency_injector import containers, providers +from dependency_injector.wiring import Provide, inject +from redis import Redis + + +class Container(containers.DeclarativeContainer): + config = providers.Configuration() + + redis = providers.Singleton(Redis, config.redis_host, config.redis_port.as_int()) def main(): - client = redis.Redis(host="redis", port=6379) + container = Container() + container.wire(modules=[__name__]) + + container.config.redis_host.from_env("REDIS_HOST") + container.config.redis_port.from_env("REDIS_PORT") + + redis = container.redis() for text in ( "As you can see", @@ -17,7 +31,7 @@ def main(): time.sleep(2) message = {"user": "John", "text": text} - client.publish("messages", json.dumps(message)) + redis.publish("messages", json.dumps(message)) if __name__ == "__main__": From ec91c345dcba20d48271b9231e07d5a936759353 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:30:35 +0300 Subject: [PATCH 05/13] Remove redundant container_name's in docker-compose.yml --- examples/miniapps/faststream/docker-compose.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index f6a246db2..1a21c8177 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -3,11 +3,9 @@ name: faststream-example services: redis: - container_name: "${COMPOSE_PROJECT_NAME}-redis" image: redis consumer: - container_name: "${COMPOSE_PROJECT_NAME}-consumer" build: dockerfile: Dockerfile environment: @@ -17,7 +15,6 @@ services: entrypoint: python3 -m src.consumer producer: - container_name: "${COMPOSE_PROJECT_NAME}-producer" build: dockerfile: Dockerfile environment: From d12417bc86bab364fc3ae5aa08705444bc87eb98 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Thu, 23 Apr 2026 18:23:56 +0300 Subject: [PATCH 06/13] Add missed fix --- examples/miniapps/faststream/faststream.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/miniapps/faststream/faststream.rst b/examples/miniapps/faststream/faststream.rst index 9e267ea80..a8271f5c9 100644 --- a/examples/miniapps/faststream/faststream.rst +++ b/examples/miniapps/faststream/faststream.rst @@ -1,7 +1,7 @@ .. _faststream-example: FastStream example -============= +================== .. meta:: :keywords: Python,Dependency Injection,FastStream,Example From cae82eb86823ba2f7ff51fb84f6aa6d61ba16f91 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 24 Apr 2026 12:57:05 +0300 Subject: [PATCH 07/13] One more missed fix --- docs/examples/faststream.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/faststream.rst b/docs/examples/faststream.rst index ae8a116ff..cee72edd7 100644 --- a/docs/examples/faststream.rst +++ b/docs/examples/faststream.rst @@ -1,7 +1,7 @@ .. _faststream-example: FastStream example -============= +================== .. meta:: :keywords: Python,Dependency Injection,FastStream,Example From bcc9940c0afdcda50679e48c928367fb0a5d8899 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 24 Apr 2026 21:01:58 +0300 Subject: [PATCH 08/13] Strip src/ --- examples/miniapps/faststream/Dockerfile | 2 +- examples/miniapps/faststream/{src => }/consumer.py | 0 examples/miniapps/faststream/docker-compose.yml | 4 ++-- examples/miniapps/faststream/{src => }/producer.py | 0 examples/miniapps/faststream/src/__init__.py | 0 5 files changed, 3 insertions(+), 3 deletions(-) rename examples/miniapps/faststream/{src => }/consumer.py (100%) rename examples/miniapps/faststream/{src => }/producer.py (100%) delete mode 100644 examples/miniapps/faststream/src/__init__.py diff --git a/examples/miniapps/faststream/Dockerfile b/examples/miniapps/faststream/Dockerfile index 80854ecb8..a29c4f41b 100644 --- a/examples/miniapps/faststream/Dockerfile +++ b/examples/miniapps/faststream/Dockerfile @@ -5,6 +5,6 @@ WORKDIR /app COPY requirements.txt ./ RUN pip install -r requirements.txt -COPY ./src ./src +COPY . ./ ENV PYTHONUNBUFFERED=1 diff --git a/examples/miniapps/faststream/src/consumer.py b/examples/miniapps/faststream/consumer.py similarity index 100% rename from examples/miniapps/faststream/src/consumer.py rename to examples/miniapps/faststream/consumer.py diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index 1a21c8177..211dd3898 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -12,7 +12,7 @@ services: REDIS_URL: "redis://redis" depends_on: - redis - entrypoint: python3 -m src.consumer + entrypoint: python3 consumer.py producer: build: @@ -22,5 +22,5 @@ services: REDIS_PORT: "6379" depends_on: - consumer - entrypoint: python3 -m src.producer + entrypoint: python3 producer.py diff --git a/examples/miniapps/faststream/src/producer.py b/examples/miniapps/faststream/producer.py similarity index 100% rename from examples/miniapps/faststream/src/producer.py rename to examples/miniapps/faststream/producer.py diff --git a/examples/miniapps/faststream/src/__init__.py b/examples/miniapps/faststream/src/__init__.py deleted file mode 100644 index e69de29bb..000000000 From 55ce6873df998c53feb54ecdfc6e4889a7fc6a5c Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 24 Apr 2026 21:02:08 +0300 Subject: [PATCH 09/13] Remove duplicate faststream.rst --- examples/miniapps/faststream/faststream.rst | 42 --------------------- 1 file changed, 42 deletions(-) delete mode 100644 examples/miniapps/faststream/faststream.rst diff --git a/examples/miniapps/faststream/faststream.rst b/examples/miniapps/faststream/faststream.rst deleted file mode 100644 index a8271f5c9..000000000 --- a/examples/miniapps/faststream/faststream.rst +++ /dev/null @@ -1,42 +0,0 @@ -.. _faststream-example: - -FastStream example -================== - -.. meta:: - :keywords: Python,Dependency Injection,FastStream,Example - :description: This example demonstrates a usage of FastStream with Dependency Injector. - - -This example shows how to use ``Dependency Injector`` with `FastStream `_. - -The source code is available on the `Github `_. - -Despite ``FastStream`` uses ``FastDepends`` library for dependency injection, the integration between -``Dependency injector`` and ``FastStream`` has a small difference from already existing :ref:`fastdepends` example. - -Since ``FastStream`` also leverages function signatures to determine input data types you have to use ``Depends()`` function -with ``cast=False`` argument to make ``FastStream`` ignore your injected dependency argument in the function signature. - -Example below shows how to inject ``Counter`` class into ``FastStream`` redis handler so that it will distinguish between -message schema (``User``) and injected dependency (``Counter``) and use them both correctly. - -Listing of ``consumer.py``: - -.. literalinclude:: ../../examples/miniapps/faststream/src/consumer.py - :language: python - -Listing of ``producer.py``: - -.. literalinclude:: ../../examples/miniapps/faststream/src/producer.py - :language: python - -Sources -------- - -Explore the sources on the `Github `_. - -.. include:: ../sponsor.rst - -.. disqus:: - From 927af68047f9f07357e3fcdca1e6ff159b5188c2 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 24 Apr 2026 21:06:39 +0300 Subject: [PATCH 10/13] Remove redundant dockerfile instruction --- examples/miniapps/faststream/docker-compose.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/miniapps/faststream/docker-compose.yml b/examples/miniapps/faststream/docker-compose.yml index 211dd3898..b760e1dfd 100644 --- a/examples/miniapps/faststream/docker-compose.yml +++ b/examples/miniapps/faststream/docker-compose.yml @@ -6,8 +6,7 @@ services: image: redis consumer: - build: - dockerfile: Dockerfile + build: . environment: REDIS_URL: "redis://redis" depends_on: @@ -15,8 +14,7 @@ services: entrypoint: python3 consumer.py producer: - build: - dockerfile: Dockerfile + build: . environment: REDIS_HOST: "redis" REDIS_PORT: "6379" From 5daf6542cb1eb53c1091409243c2140fb21a60e2 Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Fri, 24 Apr 2026 21:06:47 +0300 Subject: [PATCH 11/13] Fix FastStream link --- examples/miniapps/faststream/README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/miniapps/faststream/README.rst b/examples/miniapps/faststream/README.rst index 51cd1c0d3..aef51b4f2 100644 --- a/examples/miniapps/faststream/README.rst +++ b/examples/miniapps/faststream/README.rst @@ -1,7 +1,7 @@ FastStream + Dependency Injector Example ======================================== -This is a `FastStream `_ + +This is a `FastStream `_ + `Dependency Injector `_ example application. The example application is a simple consumer that counts messages sent to redis channel by producer. From 0e45901578b6c353bcbf6cc7201003927508deff Mon Sep 17 00:00:00 2001 From: Alexey Tsivunin <35929293+birthdaysgift@users.noreply.github.com> Date: Sun, 26 Apr 2026 03:38:42 +0300 Subject: [PATCH 12/13] Fix linters --- examples/miniapps/faststream/consumer.py | 1 - examples/miniapps/faststream/producer.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/examples/miniapps/faststream/consumer.py b/examples/miniapps/faststream/consumer.py index df5ddc631..ce40e2b8b 100644 --- a/examples/miniapps/faststream/consumer.py +++ b/examples/miniapps/faststream/consumer.py @@ -65,4 +65,3 @@ async def main() -> None: if __name__ == "__main__": asyncio.run(main()) - diff --git a/examples/miniapps/faststream/producer.py b/examples/miniapps/faststream/producer.py index bd69f5ae2..5ebc59de3 100644 --- a/examples/miniapps/faststream/producer.py +++ b/examples/miniapps/faststream/producer.py @@ -2,7 +2,6 @@ import time from dependency_injector import containers, providers -from dependency_injector.wiring import Provide, inject from redis import Redis @@ -36,4 +35,3 @@ def main(): if __name__ == "__main__": main() - From e9cc01df6b449bac1a58216e5282c490555c45be Mon Sep 17 00:00:00 2001 From: ZipFile Date: Sun, 26 Apr 2026 08:47:03 +0000 Subject: [PATCH 13/13] Fix broken references in faststream example doc --- docs/examples/faststream.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/faststream.rst b/docs/examples/faststream.rst index cee72edd7..46543009f 100644 --- a/docs/examples/faststream.rst +++ b/docs/examples/faststream.rst @@ -13,7 +13,7 @@ This example shows how to use ``Dependency Injector`` with `FastStream `_. Despite ``FastStream`` uses ``FastDepends`` library for dependency injection, the integration between -``Dependency injector`` and ``FastStream`` has a small difference from already existing :ref:`fastdepends` example. +``Dependency injector`` and ``FastStream`` has a small difference from already existing :ref:`fastdepends-example`. Since ``FastStream`` also leverages function signatures to determine input data types you have to use ``Depends()`` function with ``cast=False`` argument to make ``FastStream`` ignore your injected dependency argument in the function signature. @@ -23,12 +23,12 @@ message schema (``User``) and injected dependency (``Counter``) and use them bot Listing of ``consumer.py``: -.. literalinclude:: ../../examples/miniapps/faststream/src/consumer.py +.. literalinclude:: ../../examples/miniapps/faststream/consumer.py :language: python Listing of ``producer.py``: -.. literalinclude:: ../../examples/miniapps/faststream/src/producer.py +.. literalinclude:: ../../examples/miniapps/faststream/producer.py :language: python Sources