@@ -67,14 +67,8 @@ def init(broker: AsyncBroker, app_path: str) -> None:
6767
6868 if not isinstance (app , FastAPI ):
6969 raise ValueError (f"'{ app_path } ' is not a FastAPI application." )
70- scope = {"app" : app , "type" : "http" }
7170
72- broker .add_dependency_context (
73- {
74- Request : Request (scope = scope ),
75- HTTPConnection : HTTPConnection (scope = scope ),
76- },
77- )
71+ populate_dependency_context (broker , app )
7872
7973 broker .add_event_handler (
8074 TaskiqEvents .WORKER_STARTUP ,
@@ -85,3 +79,25 @@ def init(broker: AsyncBroker, app_path: str) -> None:
8579 TaskiqEvents .WORKER_SHUTDOWN ,
8680 shutdown_event_generator (app ),
8781 )
82+
83+
84+ def populate_dependency_context (broker : AsyncBroker , app : FastAPI ) -> None :
85+ """
86+ Populate dependency context.
87+
88+ This function injects the Request and HTTPConnection
89+ into the broker's dependency context.
90+
91+ It may be need to be called manually if you are using InMemoryBroker.
92+
93+ :param broker: current broker to use.
94+ :param app: current application.
95+ """
96+ scope = {"app" : app , "type" : "http" }
97+
98+ broker .add_dependency_context (
99+ {
100+ Request : Request (scope = scope ),
101+ HTTPConnection : HTTPConnection (scope = scope ),
102+ },
103+ )
0 commit comments