5

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.)

6
  • Can you say more about the behavior you want here: is do_stuff_with(i) the only part you want done asynchronously? Commented Aug 27, 2017 at 21:00
  • No, I'm working in a framework that uses asyncio as a base and I've got to work with it Commented Aug 27, 2017 at 21:01
  • Well, you could probably use run_in_executor to advance an iterator object. So awaiting loop.run_in_executor(None, next, it) in a loop would be reasonably close to the desired behaviour. Commented Aug 27, 2017 at 21:51
  • @vaultah that works, thanks! If you want to write an answer, the final code is iter_messages = iter(my_iter);while 1:;i = await loop.run_in_executor(None, next, iter_messages) Commented Aug 27, 2017 at 22:10
  • I think a better question is "how do I get user input asynchronously" Commented Aug 27, 2017 at 23:52

1 Answer 1

4

As @vaultah says and also explained in the docs, awaiting the executor (await loop.run_in_executor(None, next, iter_messages)) is probably what you want.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.