【问题标题】:What happen at the `continue` after a async has yielded?异步产生后,“继续”会发生什么?
【发布时间】:2019-08-30 03:53:13
【问题描述】:

一旦条件满足,以下 sn-p 中的 continue 语句将暂停 while 循环。有人可以帮助我了解引擎盖下发生了什么吗?谢谢!

import asyncio

async def yield_even():
    count = 0
    while True:
        if count % 2 != 0:
            continue

        yield count

        if count > 5:
            break

        count += 1


async def main():
    async for i in yield_even():
        print(i)


if __name__ == "__main__":
    asyncio.run(main())

【问题讨论】:

    标签: python python-3.x asynchronous yield


    【解决方案1】:

    在那里你得到了无限循环,因为在这种情况下你没有增加你的count,一旦满足这个特定条件,它就会永远满足,因为没有代码通过if 将永远被执行。

    如果你有基于计数器的迭代限制,那么while True 循环也没有意义。做常规for

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2019-04-12
      • 1970-01-01
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多