In this article we’ll see how we can close generators and throw exceptions to generators. If you want to know what is a generator, sending values to generator, you can look at part I and part II of the yield series respectively.
这篇文章我们讲学习close 生成器和throw exceptions to generators
ref:https://medium.com/geekculture/yield-python-part-iii-643b7b240f1f
close
In summary, there is close() method that exits generator and puts the generator to a GEN_CLOSED state. This is done by python by handling GeneratorExit exception without a caller’s involvement. Any other kind of exception raised from a generator is seen by a caller. We can also throw exception to a generator. If a generator returns or raises an exception, it will be seen by a caller.
总之,有一个close()方法可以退出生成器并将生成器置于GEN_CLOSED状态。这是由python通过处理GeneratorExit异常而无需调用者参与来完成的。从生成器引发的任何其他类型的异常都由调用者看到。我们还可以向生成器抛出异常。如果生成器返回或引发异常,它将被调用者看到。