本文整理汇总了Python中eventlet.queue.Queue类的典型用法代码示例。如果您正苦于以下问题:Python Queue类的具体用法?Python Queue怎么用?Python Queue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: EventletInbox
class EventletInbox(object):
def __init__(self, logger=None):
''' __init__ '''
self.__inbox = EventletQueue()
if logger is None:
self._logger = getLogger('%s.EventletInbox' % __name__)
else:
self._logger = logger
def get(self):
''' get data from inbox '''
try:
result = self.__inbox.get_nowait()
except EventletEmpty:
raise EmptyInboxException
return result
def put(self, message):
''' put message to inbox '''
self.__inbox.put(message)
def __len__(self):
''' return length of inbox '''
return self.__inbox.qsize()
示例2: _make_app_iter
def _make_app_iter(self, node, source):
"""
Returns an iterator over the contents of the source (via its read
func). There is also quite a bit of cleanup to ensure garbage
collection works and the underlying socket of the source is closed.
:param source: The httplib.Response object this iterator should read
from.
:param node: The node the source is reading from, for logging purposes.
"""
try:
# Spawn reader to read from the source and place in the queue.
# We then drop any reference to the source or node, for garbage
# collection purposes.
queue = Queue(1)
spawn_n(self._make_app_iter_reader, node, source, queue,
self.app.logger.thread_locals)
source = node = None
while True:
chunk = queue.get(timeout=self.app.node_timeout)
if isinstance(chunk, bool): # terminator
success = chunk
if not success:
raise Exception(_('Failed to read all data'
' from the source'))
break
yield chunk
except Empty:
raise ChunkReadTimeout()
except (GeneratorExit, Timeout):
self.app.logger.warn(_('Client disconnected on read'))
except Exception:
self.app.logger.exception(_('Trying to send to client'))
raise
示例3: __init__
def __init__(self):
self.queue_send = Queue()
self.queue_recv = Queue()
self.appid = None
# interface
self.sock = None
示例4: LocalMailbox
class LocalMailbox(Mailbox):
def __init__(self):
self._queue = Queue()
def put(self, message):
self._queue.put(message)
def get(self):
return self._queue.get()
示例5: MethodReader
class MethodReader(object):
"""
Helper class to receive frames from the broker, combine them if
necessary with content-headers and content-bodies into complete methods.
Normally a method is represented as a tuple containing
(channel, method_sig, args, content).
In the case of a framing error, an AMQPConnectionException is placed
in the queue.
In the case of unexpected frames, a tuple made up of
(channel, AMQPChannelException) is placed in the queue.
"""
def __init__(self, source):
self.source = source
self.queue = Queue()
self.running = False
self.partial_messages = {}
# For each channel, which type is expected next
self.expected_types = defaultdict(lambda:1)
def _next_method(self):
"""
Read the next method from the source, once one complete method has
been assembled it is placed in the internal queue.
"""
while self.queue.empty():
try:
frame_type, channel, payload = self.source.read_frame()
except Exception, e:
#
# Connection was closed? Framing Error?
#
self.queue.put(e)
break
if self.expected_types[channel] != frame_type:
self.queue.put((
channel,
Exception('Received frame type %s while expecting type: %s' %
(frame_type, self.expected_types[channel])
)
))
elif frame_type == 1:
self._process_method_frame(channel, payload)
elif frame_type == 2:
self._process_content_header(channel, payload)
elif frame_type == 3:
self._process_content_body(channel, payload)
示例6: __init__
def __init__(self, id_):
self.id = id_
self._peers = {}
self.peers_info = {}
self.available_peers = []
self.main_channel = PriorityQueue()
self.data_channel = Queue(1)
self.sending_queue = Queue()
self.receiving_queue = Queue()
self.buffer = {}
# for stats
self.sent_bytes = 0
self.received_bytes = 0
self.delays = {}
示例7: __init__
def __init__(self, *args, **kwargs):
Connection.__init__(self, *args, **kwargs)
self._write_queue = Queue()
sockerr = None
addresses = socket.getaddrinfo(
self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM
)
for (af, socktype, proto, canonname, sockaddr) in addresses:
try:
self._socket = socket.socket(af, socktype, proto)
self._socket.settimeout(1.0)
self._socket.connect(sockaddr)
sockerr = None
break
except socket.error as err:
sockerr = err
if sockerr:
raise socket.error(
sockerr.errno,
"Tried connecting to %s. Last error: %s" % (
[a[4] for a in addresses], sockerr.strerror)
)
if self.sockopts:
for args in self.sockopts:
self._socket.setsockopt(*args)
self._read_watcher = eventlet.spawn(lambda: self.handle_read())
self._write_watcher = eventlet.spawn(lambda: self.handle_write())
self._send_options_message()
示例8: __init__
def __init__(self, source):
self.source = source
self.queue = Queue()
self.running = False
self.partial_messages = {}
# For each channel, which type is expected next
self.expected_types = defaultdict(lambda:1)
示例9: __init__
def __init__(self, settings):
self.temp_dir = settings['temp_dir']
self.download_path = settings['download_path']
self.connection_pool = Queue(settings['connections'])
for _ in xrange(settings['connections']):
self.connection_pool.put(NNTP(settings['host'], settings['port'], settings['username'], settings['password']))
示例10: Actor
class Actor(ActorBase):
def __init__(self, callback):
self._inbox = Queue()
self._callback = callback
self._greenlet = None
def run(self, *args, **kwargs):
greenlet_id = id(eventlet.getcurrent())
_actor_map[greenlet_id] = self
try:
self._callback(*args, **kwargs)
finally:
del _actor_map[greenlet_id]
def spawn(self, *args, **kwargs):
self._greenlet = _actor_pool.spawn(self.run, *args, **kwargs)
def link(self, func, *args, **kwargs):
if self._greenlet is None:
return
return self._greenlet.link(func, *args, **kwargs)
def unlink(self, func, *args, **kwargs):
if self._greenlet is None:
return
return self._greenlet.unlink(func, *args, **kwargs)
def cancel(self, *throw_args):
if self._greenlet is None:
return
return self._greenlet.cancel(*throw_args)
def kill(self, *throw_args):
if self._greenlet is None:
return
return self._greenlet.kill(*throw_args)
def wait(self):
if self._greenlet is None:
return
return self._greenlet.wait()
def send(self, message):
self._inbox.put(message)
def receive(self):
return self._inbox.get()
示例11: __init__
def __init__(self, *args, **kwargs):
super(Client, self).__init__(*args, **kwargs)
self.pool = eventlet.greenpool.GreenPool(DEFAULT_POOL_SIZE)
self.reader_thread = None
self.writer_thread = None
self.queue = Queue(DEFAULT_MAX_QUEUE_SIZE)
self.max_pending = MAX_PENDING
self.closing = False
示例12: LocalMailbox
class LocalMailbox(Mailbox):
def __init__(self):
self._queue = Queue()
def put(self, message):
self._queue.put(message)
def get(self):
return self._queue.get()
def encode(self):
raise NotImplementedError
@staticmethod
def decode(params):
raise NotImplementedError
示例13: __init__
def __init__(self, *args, **kwargs):
Connection.__init__(self, *args, **kwargs)
self._write_queue = Queue()
self._connect_socket()
self._read_watcher = eventlet.spawn(lambda: self.handle_read())
self._write_watcher = eventlet.spawn(lambda: self.handle_write())
self._send_options_message()
示例14: __init__
def __init__(self, logger=None):
''' __init__ '''
self.__inbox = EventletQueue()
if logger is None:
self._logger = getLogger('%s.EventletInbox' % __name__)
else:
self._logger = logger
示例15: wait
def wait(self):
"""The difference from Queue.wait: if there is an only item in the
Queue and it is an exception, raise it, but keep it in the Queue, so
that future calls to wait() will raise it again.
"""
if self.has_error() and len(self.items) == 1:
# the last item, which is an exception, raise without emptying the Queue
getcurrent().throw(*self.items[0][1])
else:
return Queue.wait(self)