generator/coroutine terminology

Jonas Wielicki jonas at wielicki.name
Mon Mar 16 08:39:18 EDT 2015


On 16.03.2015 13:02, Steven D'Aprano wrote:
> (To be honest, I'm not even sure what the use-case for close() on coroutines
> is in the first place. If you don't want to send any more items into it,
> just don't send any more items into it.)

Just like with file-likes, it is useful to clean up resources. If you
use a coroutine with the lxml.etree.xmlfile interface [1] for instance
(code quoted from the reference):

    def writer(out_stream):
        with xmlfile(out_stream) as xf:
             with xf.element('{http://etherx.jabber.org/streams}stream'):
                  try:
                      while True:
                          el = (yield)
                          xf.write(el)
                          xf.flush()
                  except GeneratorExit:
                      pass

This allows controlled (i.e. not governed by garbage collection) and
clean shutdown of a coroutine (albeit I’m not sure why they catch the
GeneratorExit here), which will close the opened <stream> tag.

regards,
jwi

   [1]: http://lxml.de/api.html#incremental-xml-generation




-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20150316/ce62f4d7/attachment.sig>


More information about the Python-list mailing list