简体   繁体   中英

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

正如@vaultah所说,并在文档中解释, awaiting executorawait loop.run_in_executor(None, next, iter_messages) )可能就是你想要的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM