本文整理汇总了Python中Queue.LifoQueue.put_nowait方法的典型用法代码示例。如果您正苦于以下问题:Python LifoQueue.put_nowait方法的具体用法?Python LifoQueue.put_nowait怎么用?Python LifoQueue.put_nowait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Queue.LifoQueue
的用法示例。
在下文中一共展示了LifoQueue.put_nowait方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ConnectionPool
# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import put_nowait [as 别名]
class ConnectionPool(object):
def __init__(
self, connection_class=Connection,
max_connections=1024, timeout=5, **connection_kwargs):
self.connection_class = connection_class
self.max_connections = max_connections
self.connection_kwargs = connection_kwargs
self.timeout = timeout
self.connections = []
self.reset()
def ensure_safe(self):
if self.pid != os.getpid():
with self.ensure_lock: # lock for concurrent threadings
if self.pid == os.getpid(): # double check
return
self.disconnect()
self.reset()
def get_connection(self):
self.ensure_safe()
try:
connection = self.queue.get(block=True, timeout=self.timeout)
except Empty:
raise ConnectionError('connection pool is full')
if not connection:
connection = self.make_connection()
return connection
def make_connection(self):
connection = self.connection_class(**self.connection_kwargs)
self.connections.append(connection)
return connection
def release(self, connection):
self.ensure_safe()
if connection.pid != self.pid:
return
try:
self.queue.put_nowait(connection)
except Full:
pass
def reset(self):
self.pid = os.getpid()
self.ensure_lock = threading.Lock()
self.disconnect()
# LifoQueue make use of released connections
self.queue = LifoQueue(self.max_connections)
self.connections = []
while True:
try:
self.queue.put_nowait(None)
except Full:
break
def disconnect(self):
for connection in self.connections:
connection.disconnect()
示例2: TileProvider
# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import put_nowait [as 别名]
#.........这里部分代码省略.........
#the TypeError occurs when the self._dirtyLayerQueue
#is already None when the thread is being shut down
#on program exit.
#This avoids a lot of warnings.
continue
try:
if timestamp > cache.layerTimestamp( stack_id, ims, tile_nr ):
img = image_req.wait()
cache.updateTileIfNecessary( stack_id, ims, tile_nr, timestamp, img )
if stack_id == self._current_stack_id and cache is self._cache:
self.changed.emit(QRectF(self.tiling.imageRects[tile_nr]))
except KeyError:
pass
finally:
self._dirtyLayerQueue.task_done()
def _refreshTile( self, stack_id, tile_no ):
try:
if self._cache.tileDirty( stack_id, tile_no ):
self._cache.setTileDirty(stack_id, tile_no, False)
img = self._renderTile( stack_id, tile_no )
self._cache.setTile( stack_id, tile_no, img, self._sims.viewVisible(), self._sims.viewOccluded() )
# refresh dirty layer tiles
for ims in self._sims.viewImageSources():
if self._cache.layerDirty(stack_id, ims, tile_no) and not self._sims.isOccluded(ims) and self._sims.isVisible(ims):
req = (ims,
tile_no,
stack_id,
ims.request(self.tiling.imageRects[tile_no]),
time.time(),
self._cache)
try:
self._dirtyLayerQueue.put_nowait( req )
except Full:
warnings.warn("Request queue full. Dropping tile refresh request. Increase queue size!")
except KeyError:
pass
def _renderTile( self, stack_id, tile_nr ):
qimg = QImage(self.tiling.imageRects[tile_nr].size(), QImage.Format_ARGB32_Premultiplied)
qimg.fill(Qt.white)
p = QPainter(qimg)
for i, v in enumerate(reversed(self._sims)):
visible, layerOpacity, layerImageSource = v
if not visible:
continue
patch = self._cache.layer(stack_id, layerImageSource, tile_nr )
if patch is not None:
p.setOpacity(layerOpacity)
p.drawImage(0,0, patch)
p.end()
return qimg
def _onLayerDirty(self, dirtyImgSrc, rect ):
if dirtyImgSrc in self._sims.viewImageSources():
visibleAndNotOccluded = self._sims.isVisible( dirtyImgSrc ) and not self._sims.isOccluded( dirtyImgSrc )
for tile_no in xrange(len(self.tiling)):
#and invalid rect means everything is dirty
if not rect.isValid() or self.tiling.tileRects[tile_no].intersected( rect ):
for ims in self._sims.viewImageSources():
self._cache.setLayerDirtyAll(ims, tile_no, True)
if visibleAndNotOccluded:
self._cache.setTileDirtyAll(tile_no, True)
示例3: ConnectionPool
# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import put_nowait [as 别名]
class ConnectionPool(object):
"""Thread-safe connection pool implementation.
``max_connections``
Maximum number of connections.
``block_timeout``
Maximum number of seconds to wait.
"""
def __init__(self, max_connections=10, block_timeout=5,
**kwds):
self.max_connections = max_connections
self.block_timeout = block_timeout
# kwds is being sent directly to dbapi, so we pop
self._pool_id = kwds.pop('pool_id')
self._logobj = kwds.pop('logobj')
self.kwds = kwds
# make sure max_connections is valid
is_valid = isinstance(max_connections, int) and \
max_connections > 0
if not is_valid:
raise ValueError('max_connections must be a positive int')
# if process id is changed, we will close all connections,
# and reinstantiate this object
self._pid = os.getpid()
# this is where we define the pool, and fill it with None values
self._pool = LifoQueue(max_connections)
while True:
try:
self._pool.put_nowait(None)
except Full:
break
# open connections
self._connections = []
def _checkpid(self):
"""Closes all connections and reinstantiates the object if pid is changed.
"""
if self._pid == os.getpid():
return
self.disconnect()
self.reinstantiate()
def _make_connection(self):
"""Creates a fresh connection.
"""
connection = psycopg2.connect(**self.kwds)
if not hasattr(connection, 'pool_id'):
connection.pool_id = self._pool_id
# we don't need transaction, so let's turn autocommit on
connection.autocommit = True
# pass the logger object if needed
if isinstance(connection, LoggingConnection):
connection.initialize(self._logobj)
_register_hstore(connection, unicode=True)
# encoding
connection.set_client_encoding('UTF8')
# timezone
cursor = connection.cursor()
cursor.execute("set timezone to 'UTC'")
cursor.close()
self._connections.append(connection)
return connection
def get_connection(self):
"""Returns a psycopg2 connection for the given shard_id.
Raises `ConnectionError` if necessary.
"""
self._checkpid()
connection = None
try:
# wait for a connection
connection = self._pool.get(block=True,
timeout=self.block_timeout)
except Empty:
# timeout
raise ConnectionError('no connection available')
# create a new connection if it is not initialized yet
if connection is None:
connection = self._make_connection()
return connection
def put_connection(self, connection):
#.........这里部分代码省略.........
示例4: TileProvider
# 需要导入模块: from Queue import LifoQueue [as 别名]
# 或者: from Queue.LifoQueue import put_nowait [as 别名]
#.........这里部分代码省略.........
if self._cache.layerDirty(stack_id, ims, tile_no) \
and not self._sims.isOccluded(ims) \
and self._sims.isVisible(ims):
rect = self.tiling.imageRects[tile_no]
dataRect = self.tiling.scene2data.mapRect(rect)
ims_req = ims.request(dataRect, stack_id[1])
if ims.direct:
# The ImageSource 'ims' is fast (it has the
# direct flag set to true) so we process
# the request synchronously here. This
# improves the responsiveness for layers
# that have the data readily available.
start = time.time()
img = ims_req.wait()
img = img.transformed(transform)
stop = time.time()
ims._layer.timePerTile(stop-start,
self.tiling.imageRects[tile_no])
self._cache.updateTileIfNecessary(
stack_id, ims, tile_no, time.time(), img )
img = self._renderTile( stack_id, tile_no )
self._cache.setTile(stack_id, tile_no,
img, self._sims.viewVisible(),
self._sims.viewOccluded() )
else:
req = (ims, transform, tile_no, stack_id,
ims_req, time.time(), self._cache)
try:
if prefetch:
self._prefetchQueue.put_nowait( req )
else:
self._dirtyLayerQueue.put_nowait( req )
except Full:
msg = " ".join(("Request queue full.",
"Dropping tile refresh request.",
"Increase queue size!"))
warnings.warn(msg)
except KeyError:
pass
def _renderTile( self, stack_id, tile_nr):
qimg = None
p = None
for i, v in enumerate(reversed(self._sims)):
visible, layerOpacity, layerImageSource = v
if not visible:
continue
patch = self._cache.layer(stack_id, layerImageSource, tile_nr )
if patch is not None:
if qimg is None:
qimg = QImage(self.tiling.imageRects[tile_nr].size(), QImage.Format_ARGB32_Premultiplied)
qimg.fill(0xffffffff) # Use a hex constant instead.
p = QPainter(qimg)
p.setOpacity(layerOpacity)
p.drawImage(0,0, patch)
if p is not None:
p.end()
return qimg