python-dependency-injector/examples/providers/singleton_scoped.py
Roman Mogylatov 0bb30f91ef
Singleton docs update (#288)
* Update docblocks of factory provider examples

* Edit singleton docs
2020-09-01 16:04:48 -04:00

33 lines
599 B
Python

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