Python3:如何非阻塞获取Queue中的item

非阻塞获取item:

1、设置get方法传入block=false,缺点是会抛异常

	from queue import Queue
	
	def get(self, block=True, timeout=None):
	    '''Remove and return an item from the queue.
	
	    If optional args 'block' is true and 'timeout' is None (the default),
	    block if necessary until an item is available. If 'timeout' is
	    a non-negative number, it blocks at most 'timeout' seconds and raises
	    the Empty exception if no item was available within that time.
	    Otherwise ('block' is false), return an item if one is immediately
	    available, else raise the Empty exception ('timeout' is ignored
	    in that case).
	    '''
    

2、通过empty方法判断

    def empty(self):
        '''Return True if the queue is empty, False otherwise (not reliable!).

        This method is likely to be removed at some point.  Use qsize() == 0
        as a direct substitute, but be aware that either approach risks a race
        condition where a queue can grow before the result of empty() or
        qsize() can be used.

        To create code that needs to wait for all queued tasks to be
        completed, the preferred technique is to use the join() method.
        '''

你可能感兴趣的:(Python3,python)