Python(模拟awaitable)

2016-09-22  本文已影响178人  JetLu

以下代码虽然现实场景中并无用途,还是作个记录吧。


import asyncio

class B:
    def __iter__(self):
        return self
    def __next__(self):
        raise StopIteration('end')


class A:
    def __await__(self):
        return B()

async def a():
    s = await A()
    print(s)



loop = asyncio.get_event_loop()
loop.run_until_complete(a())
loop.close()
上一篇 下一篇

猜你喜欢

热点阅读