Firstly, I looked at this, this and this and whilst the first has some useful information, it's not relevant here because I'm trying to iterate over values.
Here's an example of something I want to be able to do:
class BlockingIter:
def __iter__(self):
while True:
yield input()
async def coroutine():
my_iter = BlockingIter()
#Magic thing here
async for i in my_iter:
await do_stuff_with(i)
How would I go about this?
(Note, BlockingIter is in reality a library I'm using (chatexchange) so there might be a few other complications.)
do_stuff_with(i)the only part you want done asynchronously?run_in_executorto advance an iterator object. Soawaitingloop.run_in_executor(None, next, it)in a loop would be reasonably close to the desired behaviour.iter_messages = iter(my_iter);while 1:;i = await loop.run_in_executor(None, next, iter_messages)