I have an asynchronous API which I\'m using to connect and send mail to an SMTP server which has some setup and tear down to it. So it fits nicely into using a context
context
The asyncio_extras package has a nice solution for this:
import asyncio_extras @asyncio_extras.async_contextmanager async def smtp_connection(): client = SMTPAsync() ...
For Python < 3.6, you'd also need the async_generator package and replace yield client with await yield_(client).
yield client
await yield_(client)