python-dependency-injector/examples/providers/singleton_scoped.py
RK 9ed13a4023 Fix provider documentation (#440)
Make example more realistic.  This would break if you're running flask with multiple threads.
2021-03-30 13:59:06 -06:00

39 lines
808 B
Python

"""`Singleton` - Flask request scope example."""
from dependency_injector import containers, providers
from flask import Flask, current_app
class Service:
...
class Container(containers.DeclarativeContainer):
service_provider = providers.ThreadLocalSingleton(Service)
def index_view():
service_1 = current_app.container.service_provider()
service_2 = current_app.container.service_provider()
assert service_1 is service_2
print(service_1)
return 'Hello World!'
def teardown_context(request):
current_app.container.service_provider.reset()
return request
container = Container()
app = Flask(__name__)
app.container = container
app.add_url_rule('/', 'index', view_func=index_view)
app.after_request(teardown_context)
if __name__ == '__main__':
app.run()