So, as @bosnjak said, you can use async for:
async for ITEM in A_ITER:
BLOCK1
else: # optional
BLOCK2
But if you want to iterate manually, you can simply write:
it = async_iterator()
await it.__anext__()
But I wouldn’t recommend to do that.
I think that if you are going to call something an Iterator its 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
No, acutally it’s not the same. There is a difference between regular synchronous iterators and asynchronous ones. And there few reasons for that:
- Python coroutines are built on top of generators internally
- According to Zen of python, explicit is better than implicit. So that you will actually see, where code can be suspended.
That’s why it’s impossible to use iter and next with asynchronous iterators. And you cannot use them with frameworks that expects synchronous iterators. So if you are going to make your code asynchronous, you have to use asynchronous frameworks as well. Here are few of them.
Also, I would like to say a few words about iterators and generators. Iterator is a special object that has __iter__ and __next__ methods. Whereas generator is a special function containing yield expression. Every generator is an iterator, but not vice versa. The same thing is acceptable to asynchronous iterators and generators. Yes, since python 3.6 you are able to write asynchronous generators!
async def ticker(delay, to):
for i in range(to):
yield i
await asyncio.sleep(delay)
You can read PEP 525 for more details