में सीधे इंतजार कैसेMixinig async संदर्भ प्रबंधक और asyncio
async with api.open() as o:
...
और
o = await api.open()
मिश्रण करने के लिए एक समारोह में?
के बाद से पहली __aenter__
और __aexit__
के साथ वस्तु की आवश्यकता होती है, लेकिन दूसरी __await__
, जो await
बिना जनरेटर होना चाहिए की आवश्यकता है।
मेरे प्रयास किया गया था:
def AsyncContext(aenter, aexit):
class AsyncContextClass:
async def __aenter__(self):
return await aenter()
async def __aexit__(self, *args):
return await aexit(*args)
def __await__(self):
return (yield from aenter())
return AsyncContextClass()
लेकिन यह __await__
साथ अगर aenter
async def
(TypeError: cannot 'yield from' a coroutine object in a non-coroutine generator
) के साथ परिभाषित विफल रहता है।
यह @asyncio.coroutine
सजावट aenter
के लिए सजावट के साथ ठीक काम करता है, लेकिन यह «गंदा» है।