I have an asynchronous function which connects to a database. Currently my users do:
conn = await connect(uri, other_params)
I want to continue to support this, but want to additionally allow connect()
to be used as a context manager:
async with connect(uri, other_params) as conn:
pass
The difference between these two scenarios is that in the first case connect
is awaited, and in the second case it is not.
Is it possible to tell, within the body of connect
, if the coroutine was awaited or not?
My current effort at this on repl.it.