14
how to use async for?
(discuss.tchncs.de)
Welcome to the Python community on the programming.dev Lemmy instance!
Past
November 2023
October 2023
July 2023
August 2023
September 2023
Luckily the Python community is home to giants. sqlalchemy author is one such giant and he has something of interest to contribute to this conversation. But first i bore you with stuff we all already know.
Although not a class, this pops out an AsyncIterator and shows sync equivalent.
From deep in the bowels of sqlalchemy
Should print
The decorated function can be called either as
async with fn()
, orawait fn()
. This is decidedly different from what@contextlib.asynccontextmanager
supports, and the usage pattern is different as well.Above,
GeneratorExit
is caught if the function were used as anawait
. In this case, it's essential that the cleanup does not occur, so there should not be afinally
block.If
GeneratorExit
is not invoked, this means we're in__aexit__
and we were invoked as a context manager, and cleanup should proceed.So instead of a class with
__anext__
and__aiter__
an asyncstartablecontext withyield from
could be a possible alternative.oh wow thanks!