I'm trying the following:
class Payload_Session_Generator: def __init__(self): pass async def __anext__(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload else: StopAsyncIteration def __aiter__(self): return selfThis is then passed as an instance to a different function and the __aiter__ method is called explicitly and _iter is an object of the above class:
chunk = await self._iter.__anext__()This then yields the following error:
1TypeError: object async_generator can't be used in 'await' expression
2 Answers
Change your code like
class PayloadSessionGenerator: def __init__(self): pass async def __aiter__(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload else: StopAsyncIterationtry next call
x = Payload_Session_Generator()
chunk = await x.__anext__() 0 An asynchronous generator is similar to a regular generator: __anext__ must provide one element each time it is called. Since an (async) generator is an (async) iterator, it must return the object itself.
As such, __anext__ should use return instead of yield:
class Payload_Session_Generator: async def __anext__(self): async for payload in generate_fb_payload(): if type(payload) != str: return payload # return one element else: raise StopAsyncIteration # raise at end of iteration def __aiter__(self): return selfNote that if a class uses just __anext__ and __aiter__, it is simpler to write a proper async generator. An async generator is defined using async def and yield in the body:
async def payload_session_generator(self): async for payload in generate_fb_payload(): if type(payload) != str: yield payload # yield each element else: break # exiting ends iteration