Implement async resources initialization in container

This commit is contained in:
Roman Mogylatov 2020-12-07 19:37:04 -05:00
parent 8505440677
commit 45cd887a37
4 changed files with 2147 additions and 1497 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,17 @@
from types import ModuleType
from typing import Type, Dict, Tuple, Optional, Any, Union, ClassVar, Callable as _Callable, Iterable, TypeVar
from typing import (
Type,
Dict,
Tuple,
Optional,
Any,
Union,
ClassVar,
Callable as _Callable,
Iterable,
TypeVar,
Awaitable,
)
from .providers import Provider
@ -25,8 +37,8 @@ class Container:
def resolve_provider_name(self, provider_to_resolve: Provider) -> Optional[str]: ...
def wire(self, modules: Optional[Iterable[ModuleType]] = None, packages: Optional[Iterable[ModuleType]] = None) -> None: ...
def unwire(self) -> None: ...
def init_resources(self) -> None: ...
def shutdown_resources(self) -> None: ...
def init_resources(self) -> Optional[Awaitable]: ...
def shutdown_resources(self) -> Optional[Awaitable]: ...
class DynamicContainer(Container): ...

View File

@ -1,7 +1,13 @@
"""Containers module."""
import inspect
import sys
try:
import asyncio
except ImportError:
asyncio = None
import six
from .errors import Error
@ -216,17 +222,33 @@ class DynamicContainer(object):
def init_resources(self):
"""Initialize all container resources."""
futures = []
for provider in self.providers.values():
if not isinstance(provider, Resource):
continue
provider.init()
resource = provider.init()
if inspect.isawaitable(resource):
futures.append(resource)
if futures:
return asyncio.gather(*futures)
def shutdown_resources(self):
"""Shutdown all container resources."""
futures = []
for provider in self.providers.values():
if not isinstance(provider, Resource):
continue
provider.shutdown()
shutdown = provider.shutdown()
if inspect.isawaitable(shutdown):
futures.append(shutdown)
if futures:
return asyncio.gather(*futures)
class DeclarativeContainerMetaClass(type):

View File

@ -2,6 +2,19 @@
import unittest2 as unittest
# Runtime import to get asyncutils module
import os
_TOP_DIR = os.path.abspath(
os.path.sep.join((
os.path.dirname(__file__),
'../',
)),
)
import sys
sys.path.append(_TOP_DIR)
from asyncutils import AsyncTestCase
from dependency_injector import (
containers,
providers,
@ -233,5 +246,55 @@ class DeclarativeContainerInstanceTests(unittest.TestCase):
self.assertEqual(_init2.shutdown_counter, 2)
class AsyncResourcesInitializationTest(AsyncTestCase):
@unittest.skipIf(sys.version_info[:2] <= (3, 5), 'Async test')
def test_async_init_resources(self):
async def _init1():
_init1.init_counter += 1
yield
_init1.shutdown_counter += 1
_init1.init_counter = 0
_init1.shutdown_counter = 0
async def _init2():
_init2.init_counter += 1
yield
_init2.shutdown_counter += 1
_init2.init_counter = 0
_init2.shutdown_counter = 0
class Container(containers.DeclarativeContainer):
resource1 = providers.Resource(_init1)
resource2 = providers.Resource(_init2)
container = Container()
self.assertEqual(_init1.init_counter, 0)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 0)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.init_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 0)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 0)
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 1)
self.assertEqual(_init1.shutdown_counter, 1)
self.assertEqual(_init2.init_counter, 1)
self.assertEqual(_init2.shutdown_counter, 1)
self._run(container.init_resources())
self._run(container.shutdown_resources())
self.assertEqual(_init1.init_counter, 2)
self.assertEqual(_init1.shutdown_counter, 2)
self.assertEqual(_init2.init_counter, 2)
self.assertEqual(_init2.shutdown_counter, 2)
if __name__ == '__main__':
unittest.main()