2014-12-21 57 views
1

本節摘自Python doc。它的文檔字符串表示函數是non-blocking(例如#非阻塞字典迭代器),這是我不明白的地方。什麼是非阻塞生成器

def iter_except(func, exception, first=None): 
    """ Call a function repeatedly until an exception is raised. 

    Converts a call-until-exception interface to an iterator interface. 
    Like builtins.iter(func, sentinel) but uses an exception instead 
    of a sentinel to end the loop. 

    Examples: 
     iter_except(functools.partial(heappop, h), IndexError) # priority queue iterator 
     iter_except(d.popitem, KeyError)       # non-blocking dict iterator 
     iter_except(d.popleft, IndexError)      # non-blocking deque iterator 
     iter_except(q.get_nowait, Queue.Empty)     # loop over a producer Queue 
     iter_except(s.pop, KeyError)        # non-blocking set iterator 

    """ 
    try: 
     if first is not None: 
      yield first()   # For database APIs needing an initial cast to db.first() 
     while 1: 
      yield func() 
    except exception: 
     pass 

在我看來只是一個generator function做同樣的事情爲iter()

據我所知,non-blocking的意思是異步或並行計算,或者當你在多線程下襬脫lock。 片段處於同步執行狀態。這裏的non-blocking是什麼意思?

+1

我想他們的意思是'd'可以在迭代過程中被其他代碼改變,所以這個函數不會'阻塞'它。使用'iter'並更改底層容器可能會導致不可預知的結果。 – zch

回答

0

據我所知 - non-blocking這裏用作not breaking the code execution when a specific error occurs

因此,這些生成器不會在異常發生時阻止我的代碼運行。