什么是无阻塞发电机

时间:2014-12-21 11:54:24

标签: python generator nonblocking coroutine

此摘录来自Python doc。 doc字符串表示函数是non-blocking(例如#non-blocking dict iterator),这是我不理解的地方。

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 functioniter()做同样的事情。

据我所知,non-blocking表示异步或并行计算,或者当您在多线程中摆脱lock时。 该代码段处于同步执行状态。这里non-blocking是什么意思?

1 个答案:

答案 0 :(得分:0)

根据我的理解 - non-blocking此处用作not breaking the code execution when a specific error occurs

所以这些发电机不会阻止'我的代码在发生异常时运行。