mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-11 17:10:57 +03:00
Add example
This commit is contained in:
parent
79df45ff48
commit
b7762f99e8
32
examples/providers/resource/resource.py
Normal file
32
examples/providers/resource/resource.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user