Add example

This commit is contained in:
Roman Mogylatov 2020-10-22 22:34:02 -04:00
parent 79df45ff48
commit b7762f99e8

View File

@ -0,0 +1,32 @@
"""`Resource` provider example."""
import concurrent.futures
from dependency_injector import containers, providers
def init_threat_pool(max_workers: int):
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=max_workers)
yield thread_pool
thread_pool.shutdown(wait=True)
class Container(containers.DeclarativeContainer):
config = providers.Configuration()
thread_pool = providers.Resource(
init_threat_pool,
max_workers=config.max_workers,
)
if __name__ == '__main__':
container = Container(config={'max_workers': 4})
container.init_resources()
thread_pool = container.thread_pool()
assert list(thread_pool.map(str, range(3))) == ['0', '1', '2']
container.shutdown_resources()