Asynchronous context manager
<p>I have an <a href="https://github.com/vuamitom/tornado-smtpclient" rel="noreferrer">asynchronous API</a> 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 <code>contextmanager</code> from Python 3's <code>contextlib</code>.</p>

<p>Though, I don't know if it's possible write because they both use the generator syntax to write.</p>

<p>This might demonstrate the problem (contains a mix of yield-base and async-await syntax to demonstrate the difference between async calls and yields to the context manager).</p>

<pre><code>@contextmanager
async def smtp_connection():
    client = SMTPAsync()
    ...

    try:
        await client.connect(smtp_url, smtp_port)
        await client.starttls()
        await client.login(smtp_username, smtp_password)
        yield client
    finally:
        await client.quit()
</code></pre>

<p>Is this kind of thing possible within python currently? and how would I use a <code>with</code> <code>as</code> statement if it is? If not is there a alternative way I could achieve this - maybe using the old style context manager?</p>