muddyfish
muddyfish

Reputation: 3660

Using asyncio with blocking code

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

Upvotes: 5

Views: 1031

Answers (1)

Amin Etesamian
Amin Etesamian

Reputation: 3699

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.

Upvotes: 4

Related Questions