What are use-cases for a coroutine?

前端 未结 7 1849
你的背包
你的背包 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:27

    As a more-specific example in the producer/consumer line, something as simple as the humble batch reporting program could actually use co-routines.

    The key hint for that example is having a nontrivial work to consume input data (e.g. parsing data or accumulating charges and payments on an account), and non-trivial work to produce the output. When you have these characteristics:

    • It is easy to organize/understand the input-side code if you can "emit" units of work at various places.
    • It is likewise easy to organize/understand the output-side code if it can "grab" the next unit of work in a nested control structure.

    then coroutines and queues are both nice techniques to have at your disposal.

提交回复
热议问题