Drain or discard a generator without looping?

后端 未结 3 924
栀梦
栀梦 2021-01-14 13:58

In an exception handler for a CSP style process, I need to read and discard the entire contents of a channel in order to allow other processes that are blocking to send to i

3条回答
  •  既然无缘
    2021-01-14 14:34

    You might try:

    reduce(lambda _, __: None, chan)
    

    But honestly I don't think you're going to do much better than the plain loop. "channel" suggests I/O which is going to be the bottleneck anyway.

提交回复
热议问题