Consuming two iterators in parallel

前端 未结 1 1628
南方客
南方客 2021-01-21 09:41

Suppose I have two iterators, and I want to compute

fancyoperation1(iter1), fancyoperation2(iter2)

Normally, I would simply use fancyoper

相关标签:
1条回答
  • 2021-01-21 10:39

    You absolutely can use coroutines for this, it's just slightly less convenient (but on the bright side, you can keep them separated and can leave most code unaltered). Change the fancy operations to be parameterless and repeatedly use yield (as expression) to fetch data instead of accepting a parameter and iterating over it. In other words, change this:

    def fancyoperation1(it):
        for x in it:
            ...
        cleanup()
    
    # into something like this
    
    def fancyoperation1():
        while True:
            try:
                x = yield
            except GeneratorExit:
                break
            ...
        cleanup()
    

    Of course, it's easier if there is no post-iteration clean up to be done. You can use these like this (assuming iter1, iter2 = tee(underlying_iter)):

    f1, f2 = fancyoperation1(), fancyoperation2()
    f1.send(None) # start coroutines
    f2.send(None)
    
    for x in underlying_iterator:
        f1.send(x)
        f2.send(x)
    f1.close()
    f2.close()
    
    0 讨论(0)
提交回复
热议问题