mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
Process closings in any case
This commit is contained in:
parent
cc2304e46e
commit
d001b2930e
File diff suppressed because it is too large
Load Diff
|
@ -25,17 +25,17 @@ def _get_sync_patched(fn, patched: PatchedCallable):
|
|||
if arg_key not in kwargs or isinstance(kwargs[arg_key], _Marker):
|
||||
to_inject[arg_key] = provider()
|
||||
|
||||
result = fn(*args, **to_inject)
|
||||
try:
|
||||
return fn(*args, **to_inject)
|
||||
finally:
|
||||
if patched.closing:
|
||||
for arg_key, provider in patched.closing.items():
|
||||
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
provider.shutdown()
|
||||
|
||||
if patched.closing:
|
||||
for arg_key, provider in patched.closing.items():
|
||||
if arg_key in kwargs and not isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
provider.shutdown()
|
||||
|
||||
return result
|
||||
return _patched
|
||||
|
||||
|
||||
|
@ -53,7 +53,7 @@ async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dic
|
|||
provide = provider()
|
||||
if provider.is_async_mode_enabled():
|
||||
to_inject_await.append((arg_key, provide))
|
||||
elif _isawaitable(provide):
|
||||
elif _is_awaitable(provide):
|
||||
to_inject_await.append((arg_key, provide))
|
||||
else:
|
||||
to_inject[arg_key] = provide
|
||||
|
@ -62,25 +62,24 @@ async def _async_inject(object fn, tuple args, dict kwargs, dict injections, dic
|
|||
async_to_inject = await asyncio.gather(*(provide for _, provide in to_inject_await))
|
||||
for provide, (injection, _) in zip(async_to_inject, to_inject_await):
|
||||
to_inject[injection] = provide
|
||||
try:
|
||||
return await fn(*args, **to_inject)
|
||||
finally:
|
||||
if closings:
|
||||
for arg_key, provider in closings.items():
|
||||
if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
shutdown = provider.shutdown()
|
||||
if _is_awaitable(shutdown):
|
||||
to_close_await.append(shutdown)
|
||||
|
||||
result = await fn(*args, **to_inject)
|
||||
|
||||
if closings:
|
||||
for arg_key, provider in closings.items():
|
||||
if arg_key in kwargs and isinstance(kwargs[arg_key], _Marker):
|
||||
continue
|
||||
if not isinstance(provider, providers.Resource):
|
||||
continue
|
||||
shutdown = provider.shutdown()
|
||||
if _isawaitable(shutdown):
|
||||
to_close_await.append(shutdown)
|
||||
|
||||
await asyncio.gather(*to_close_await)
|
||||
|
||||
return result
|
||||
await asyncio.gather(*to_close_await)
|
||||
|
||||
|
||||
cdef bint _isawaitable(object instance):
|
||||
|
||||
cdef bint _is_awaitable(object instance):
|
||||
"""Return true if object can be passed to an ``await`` expression."""
|
||||
return (isinstance(instance, types.CoroutineType) or
|
||||
isinstance(instance, types.GeneratorType) and
|
||||
|
|
Loading…
Reference in New Issue
Block a user