Python, yield 에서 협 정 까지

#codomg:UTF-8
#  :http://www.runoob.com/w3cnote/python-yield-used-analysis.html
"""   Python    Fibonacci  """
def Fibonacci_1(N):
"""     1.0
             N      
          :        new bee  , print      
"""
    n, a, b = 0, 0, 1
    while n < N:
        print b
        a, b = b, a+b
        n=n+1

def Fibonacci_2(N):
"""     2.0
           N       
          :                ,      N     ,    
"""
    n, a, b = 0, 0, 1
    L=[]
    while n < N:
        L.append(b)
        a, b = b, a+b
        n = n+1
    return L

class Fibonacci_3(object)):
"""     3.0
                      
          :                  ,             ,       ?
"""
    def __init__(self, N):
        self.N = N
        self.n, self.a, self.b = 0, 0, 1
    
    def __iter__(self):
        return self
    
    def next(self):
        if self.n < self.N:
            r = self.b
            self.a, self.b = self.b, self.a+self.b
            self.n = self.n + 1
            return r
        raise StopIteration()

def Fibonacci_4(N):
"""     4.0
        yield  
           ,  1.0     ,             ,        ,      
"""
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a+b
        n = n+1

#                     
#https://www.zhihu.com/question/20511233/answer/24459911
"""  ,         
        ,          ,         。
           ,          ,       ,      ,      
            ,     
                      
           ,              
    yield            
    yield            ,  return   
                  
         ,C             
             ,             ,             
       “  ”
      CPU     。
            ,   ,       ,          
            ,   ,      ,         ,                    CPU   ,        
                   ,        ,          ,              
                ,        ,                 
            ,                   
             
             
    k8s          ,            ,          
       ,          。
"""

"""              ,           """
#  :https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868328689835ecd883d910145dfa8227b539725e5ed000
import time

def consumer():
    r = ''
    while True:
        n = yield r
        if not n:
            return
        print('[CONSUMER] Consuming %s...' % n)
        time.sleep(1)
        r = '200 OK'

def produce(c):
    c.next()
    n = 0
    while n < 5:
        n = n + 1
        print('[PRODUCER] Producing %s...' % n)
        r = c.send(n)
        print('[PRODUCER] Consumer return: %s' % r)
    c.close()

if __name__=='__main__':
    c = consumer()
    produce(c)

"""    
    [PRODUCER] Producing 1...
    [CONSUMER] Consuming 1...
    [PRODUCER] Consumer return: 200 OK
    [PRODUCER] Producing 2...
    [CONSUMER] Consuming 2...
    [PRODUCER] Consumer return: 200 OK
    [PRODUCER] Producing 3...
    [CONSUMER] Consuming 3...
    [PRODUCER] Consumer return: 200 OK
    [PRODUCER] Producing 4...
    [CONSUMER] Consuming 4...
    [PRODUCER] Consumer return: 200 OK
    [PRODUCER] Producing 5...
    [CONSUMER] Consuming 5...
    [PRODUCER] Consumer return: 200 OK

            yield,consumer    generator(     ),   consumer  produce :

        c.next()    ;

      ,       ,  c.send(n)   consumer  ;

    consumer  yield    ,  ,   yield     ;

    produce  consumer     ,         ;

    produce      ,  c.close()  consumer,      。

          ,       ,produce consumer      ,    “  ”,           。

        Donald Knuth           :

    “            。”
"""

좋은 웹페이지 즐겨찾기