本文整理汇总了Python中queue.LifoQueue.get_nowait方法的典型用法代码示例。如果您正苦于以下问题:Python LifoQueue.get_nowait方法的具体用法?Python LifoQueue.get_nowait怎么用?Python LifoQueue.get_nowait使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类queue.LifoQueue
的用法示例。
在下文中一共展示了LifoQueue.get_nowait方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: range
# 需要导入模块: from queue import LifoQueue [as 别名]
# 或者: from queue.LifoQueue import get_nowait [as 别名]
str="abcdefghijk"
import random
#Queue
q=Queue(10)
for i in range(10):
q.put(random.choice(str))
print("size=",q.qsize())
while not q.empty():
print(q.get())
q.task_done()
#Lifo Queue
print("-"*10,"lifo_queue","-"*10)
lifoq=LifoQueue(10)
for i in range(10):
lifoq.put_nowait(random.choice(str))
while not lifoq.empty():
print(lifoq.get_nowait())
lifoq.task_done()
#Priority Queue
print("-"*10,"priority queue","-"*10)
pq=PriorityQueue(10)
for i in range(10):
pq.put_nowait(random.choice(str))
while not pq.empty():
print(pq.get_nowait())
pq.task_done()
示例2: ResourcePool
# 需要导入模块: from queue import LifoQueue [as 别名]
# 或者: from queue.LifoQueue import get_nowait [as 别名]
class ResourcePool(metaclass=ABCMeta):
"""Base class that provides a pool of resources."""
def __init__(self, min_size, max_size=None):
if not isinstance(min_size, int):
raise TypeError('min_size should be int.')
if max_size and not isinstance(max_size, int):
raise TypeError('max_size should be int.')
if min_size < 0:
raise ValueError('min_size should be greater than 0.')
if max_size and max_size < min_size:
raise ValueError('max_size should be greater or equal to min_size.')
self.__min_size = min_size
self.__max_size = max_size or -1
self.__size = 0
self.__size_lock = threading.Lock()
self.__closed = False
self.__pool = LifoQueue()
@property
def max_size(self):
"""Gets the maximum number of resources."""
return self.__max_size
@property
def min_size(self):
"""Gets the minimum number of resources."""
return self.__min_size
def acquire(self):
"""Acquires a resource."""
if self.__closed:
raise RuntimeError('Acquire on closed pool')
try:
return self.__pool.get_nowait()
except Empty:
if not self.__inc_size():
raise LimitedExceeded('Limit of channels exceeded.')
try:
return self._create_resource()
except:
self.__dec_size()
raise
def release(self, resource):
"""Release resource so it can be used by another thread."""
if self.__size > self.__min_size:
self.__dec_size()
self._close_resource(resource)
else:
if self._validate_resource(resource):
self.__pool.put_nowait(resource)
else:
self.__dec_size()
self._close_resource(resource)
def close(self):
"""Closes and removes all resources in the pool."""
self.__closed = True
while 1:
try:
resource = self.__pool.get_nowait()
self._close_resource(resource)
except Empty:
break
self.__size = 0
def __inc_size(self):
"""Increments the number of resources alive."""
if self.__max_size == -1:
self.__size += 1
return True
with self.__size_lock:
if self.__size < self.__max_size:
self.__size += 1
return True
else:
return False
def __dec_size(self):
"""Decrements the number of resources alive."""
if self.__max_size == -1:
self.__size -= 1
return True
with self.__size_lock:
self.__size -= 1
return True
#.........这里部分代码省略.........