33from concurrent .futures import Executor
44from logging import getLogger
55from time import time
6- from typing import Any , Callable , Dict , Optional , Set , Union , get_type_hints
6+ from typing import Any , Callable , Dict , List , Optional , Set , Union , get_type_hints
77
88import anyio
99from taskiq_dependencies import DependencyGraph
1010
1111from taskiq .abc .broker import AckableMessage , AsyncBroker
1212from taskiq .abc .middleware import TaskiqMiddleware
1313from taskiq .context import Context
14- from taskiq .exceptions import NoResultError , RejectError
14+ from taskiq .exceptions import NoResultError
1515from taskiq .message import TaskiqMessage
1616from taskiq .receiver .params_parser import parse_params
1717from taskiq .result import TaskiqResult
2222QUEUE_DONE = b"-1"
2323
2424
25- def _run_sync (target : Callable [..., Any ], message : TaskiqMessage ) -> Any :
25+ def _run_sync (
26+ target : Callable [..., Any ],
27+ args : List [Any ],
28+ kwargs : Dict [str , Any ],
29+ ) -> Any :
2630 """
2731 Runs function synchronously.
2832
2933 We use this function, because
3034 we cannot pass kwargs in loop.run_with_executor().
3135
3236 :param target: function to execute.
33- :param message: received message from broker.
37+ :param args: list of function's args.
38+ :param kwargs: dict of function's kwargs.
3439 :return: result of function's execution.
3540 """
36- return target (* message . args , ** message . kwargs )
41+ return target (* args , ** kwargs )
3742
3843
3944class Receiver :
@@ -124,20 +129,16 @@ async def callback( # noqa: C901, WPS213, WPS217
124129 taskiq_msg .task_name ,
125130 taskiq_msg .task_id ,
126131 )
132+
133+ # If broker has an ability to ack messages.
134+ if isinstance (message , AckableMessage ):
135+ await maybe_awaitable (message .ack ())
136+
127137 result = await self .run_task (
128138 target = self .broker .available_tasks [taskiq_msg .task_name ].original_func ,
129139 message = taskiq_msg ,
130140 )
131141
132- # If broker has an ability to ack or reject messages.
133- if isinstance (message , AckableMessage ):
134- # If we received an error for negative acknowledgement.
135- if message .reject is not None and isinstance (result .error , RejectError ):
136- await maybe_awaitable (message .reject ())
137- # Otherwise we positively acknowledge the message.
138- else :
139- await maybe_awaitable (message .ack ())
140-
141142 for middleware in self .broker .middlewares :
142143 if middleware .__class__ .post_execute != TaskiqMiddleware .post_execute :
143144 await maybe_awaitable (middleware .post_execute (taskiq_msg , result ))
@@ -182,14 +183,18 @@ async def run_task( # noqa: C901, WPS210
182183 """
183184 loop = asyncio .get_running_loop ()
184185 returned = None
185- found_exception = None
186+ found_exception : "Optional[BaseException]" = None
186187 signature = None
187188 if self .validate_params :
188189 signature = self .task_signatures .get (message .task_name )
189190 dependency_graph = self .dependency_graphs .get (message .task_name )
190191 parse_params (signature , self .task_hints .get (message .task_name ) or {}, message )
191192
192193 dep_ctx = None
194+ # Kwargs are defined in another variable,
195+ # because we want to update them with
196+ # kwargs resolved by dependency injector.
197+ kwargs = {}
193198 if dependency_graph :
194199 # Create a context for dependency resolving.
195200 broker_ctx = self .broker .custom_dependency_context
@@ -201,25 +206,34 @@ async def run_task( # noqa: C901, WPS210
201206 )
202207 dep_ctx = dependency_graph .async_ctx (broker_ctx )
203208 # Resolve all function's dependencies.
204- dep_kwargs = await dep_ctx .resolve_kwargs ()
205- for key , val in dep_kwargs .items ():
206- if key not in message .kwargs :
207- message .kwargs [key ] = val
209+ kwargs = await dep_ctx .resolve_kwargs ()
210+
211+ # We udpate kwargs with kwargs from network.
212+ kwargs .update (message .kwargs )
213+
208214 # Start a timer.
209215 start_time = time ()
210216 try :
211- # If the function is a coroutine we await it.
217+ # If the function is a coroutine, we await it.
212218 if asyncio .iscoroutinefunction (target ):
213- returned = await target (* message .args , ** message . kwargs )
219+ returned = await target (* message .args , ** kwargs )
214220 else :
215- # If this is a synchronous function we
221+ # If this is a synchronous function, we
216222 # run it in executor.
217223 returned = await loop .run_in_executor (
218224 self .executor ,
219225 _run_sync ,
220226 target ,
221- message ,
227+ message .args ,
228+ kwargs ,
222229 )
230+ except NoResultError as no_res_exc :
231+ found_exception = no_res_exc
232+ logger .warning (
233+ "Task %s with id %s skipped setting result." ,
234+ message .task_name ,
235+ message .task_id ,
236+ )
223237 except BaseException as exc : # noqa: WPS424
224238 found_exception = exc
225239 logger .error (
0 commit comments