将 asyncio 与阻塞代码一起使用

Using asyncio with blocking code

首先,我查看了 this, and this,虽然第一个有一些有用的信息,但它与此处无关,因为我正在尝试迭代值。

这是我希望能够做的事情的示例:

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)

我该怎么做?

(请注意,BlockingIter 实际上是我正在使用的库 (chatexchange),因此可能还有一些其他问题。)

正如@vaultah 所说并在 docs 中解释的那样,awaiting executor (await loop.run_in_executor(None, next, iter_messages)) 可能就是您想要的。