What are use-cases for a coroutine?

前端 未结 7 1862
你的背包
你的背包 2021-01-30 12:46

The concept of a coroutine sounds very interesting, but I don\'t know, if it makes sense in a real productive environment? What are use-cases for coroutines, that can be solved

7条回答
  •  故里飘歌
    2021-01-30 13:26

    Lots of them, for example:

    grep TODO *.c *.h | wc -l
    

    The pipeline above is exactly a coroutine: the grep command generates a sequence of lines which go to a buffer, the wc command "eats them up"; if the buffer fills, the grep "blocks" until the buffer empties, and if the buffer is empty, the wc command waits for new input.

    The thing about coroutines is that they are most often now used either in more constrained patterns, like the Python generators mentioned, or as pipelines.

    If you want to look more at them, see the Wikipedia articles, especially on coroutines and iterators.

提交回复
热议问题