mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-05-23 14:59:08 +03:00
Add docs section on async resources
This commit is contained in:
parent
68732385d9
commit
c2bddfb3de
|
@ -235,4 +235,32 @@ The example above produces next output:
|
|||
Shutdown service
|
||||
127.0.0.1 - - [29/Oct/2020 22:39:41] "GET / HTTP/1.1" 200 -
|
||||
|
||||
Asynchronous resources
|
||||
----------------------
|
||||
|
||||
Resource provider supports asynchronous initialization and shutdown. To use it you need to make the
|
||||
initializer asynchronous using one of the following ways:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
async def init():
|
||||
return await connect()
|
||||
|
||||
|
||||
async def init():
|
||||
connection = await connect()
|
||||
yield connection
|
||||
await connection.close()
|
||||
|
||||
|
||||
class Connection(resources.AsyncResource):
|
||||
|
||||
async def init():
|
||||
yield await connect()
|
||||
|
||||
async def shutdown(connection):
|
||||
await connection.close()
|
||||
|
||||
Initialization, shutdown, injections to the providers and wiring work the same way as for sync resources.
|
||||
|
||||
.. disqus::
|
||||
|
|
Loading…
Reference in New Issue
Block a user