In Python you can write a generator that is iterable like:
def generate(count):
for x in range(count):
yield x
# as an iterator you can apply the function next() to get the values.
it = generate(10)
r0 = next(it)
r1 = next(it) ...
When trying to use an async iterator, you get the 'yield inside async' error. The suggested solution is to implement your own generator:
class async_generator:
def __aiter__(self):
return self
async def __anext__(self):
await asyncio.sleep()
return random.randint(0, 10)
# But when you try to get the next element
it = async_generator(10)
r0 = next(it)
You get the error "async_generator" object is not an iterator
I think that if you are going to call something an Iterator it's because it has exactly the same interface, so I can just write async iterators and use on a framework that relies heavily on next() calls. Any new Python capability is pointless if you need to rewrite your entire code to be able to use async.
Am I missing something?
Thanks!