本文整理汇总了Python中tornado.queues.Queue.empty方法的典型用法代码示例。如果您正苦于以下问题:Python Queue.empty方法的具体用法?Python Queue.empty怎么用?Python Queue.empty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.queues.Queue
的用法示例。
在下文中一共展示了Queue.empty方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: BatchedStream
# 需要导入模块: from tornado.queues import Queue [as 别名]
# 或者: from tornado.queues.Queue import empty [as 别名]
class BatchedStream(object):
""" Mostly obsolete, see BatchedSend """
def __init__(self, stream, interval):
self.stream = stream
self.interval = interval / 1000.0
self.last_transmission = default_timer()
self.send_q = Queue()
self.recv_q = Queue()
self._background_send_coroutine = self._background_send()
self._background_recv_coroutine = self._background_recv()
self._broken = None
self.pc = PeriodicCallback(lambda: None, 100)
self.pc.start()
@gen.coroutine
def _background_send(self):
with log_errors():
while True:
msg = yield self.send_q.get()
if msg == "close":
break
msgs = [msg]
now = default_timer()
wait_time = self.last_transmission + self.interval - now
if wait_time > 0:
yield gen.sleep(wait_time)
while not self.send_q.empty():
msgs.append(self.send_q.get_nowait())
try:
yield write(self.stream, msgs)
except StreamClosedError:
self.recv_q.put_nowait("close")
self._broken = True
break
if len(msgs) > 1:
logger.debug("Batched messages: %d", len(msgs))
for _ in msgs:
self.send_q.task_done()
@gen.coroutine
def _background_recv(self):
with log_errors():
while True:
try:
msgs = yield read(self.stream)
except StreamClosedError:
self.recv_q.put_nowait("close")
self.send_q.put_nowait("close")
self._broken = True
break
assert isinstance(msgs, list)
if len(msgs) > 1:
logger.debug("Batched messages: %d", len(msgs))
for msg in msgs:
self.recv_q.put_nowait(msg)
@gen.coroutine
def flush(self):
yield self.send_q.join()
@gen.coroutine
def send(self, msg):
if self._broken:
raise StreamClosedError("Batch Stream is Closed")
else:
self.send_q.put_nowait(msg)
@gen.coroutine
def recv(self):
result = yield self.recv_q.get()
if result == "close":
raise StreamClosedError("Batched Stream is Closed")
else:
raise gen.Return(result)
@gen.coroutine
def close(self):
yield self.flush()
raise gen.Return(self.stream.close())
def closed(self):
return self.stream.closed()
示例2: AsyncConnection
# 需要导入模块: from tornado.queues import Queue [as 别名]
# 或者: from tornado.queues.Queue import empty [as 别名]
#.........这里部分代码省略.........
def __on_connect(self, result):
log.debug("Connection establishment")
self.__connected = True
self.__io_loop.add_callback(self._loop)
@coroutine
def _loop(self):
log.debug("Starting queue loop")
while self.__connected:
while self.__has_active_cursor or self.__connection.isexecuting():
yield sleep(0.001)
func, future = yield self.__queue.get()
result = func()
if isinstance(result, Future):
result = yield result
self.__io_loop.add_callback(future.set_result, result)
yield self.__wait()
@coroutine
def __wait(self):
log.debug("Waiting for events")
while not (yield sleep(0.001)):
try:
state = self.__connection.poll()
except QueryCanceledError:
yield sleep(0.1)
continue
f = Future()
def resolve(fileno, io_op):
if f.running():
f.set_result(True)
self.__io_loop.remove_handler(fileno)
if state == psycopg2.extensions.POLL_OK:
raise Return(True)
elif state == psycopg2.extensions.POLL_READ:
self.__io_loop.add_handler(self.__connection.fileno(), resolve, IOLoop.READ)
yield f
elif state == psycopg2.extensions.POLL_WRITE:
self.__io_loop.add_handler(self.__connection.fileno(), resolve, IOLoop.WRITE)
yield f
def __on_cursor_open(self, cursor):
self.__has_active_cursor = True
log.debug("Opening cursor")
def __on_cursor_close(self, cursor):
self.__has_active_cursor = False
log.debug("Closing active cursor")
def cursor(self, **kwargs):
f = Future()
self.__io_loop.add_callback(
self.__queue.put,
(
functools.partial(
AsyncCursor,
self.__connection,
self.__thread_pool,
self.__wait,
on_open=self.__on_cursor_open,
on_close=self.__on_cursor_close,
**kwargs
),
f,
),
)
return f
def cancel(self):
return self.__thread_pool.submit(self.__connection.cancel)
def close(self):
self.__has_active_cursor = True
@coroutine
def closer():
while not (yield self.__queue.empty()):
func, future = yield self.__queue.get()
future.set_exception(psycopg2.Error("Connection closed"))
self.__io_loop.add_callback(self.__connection.close)
def __futurize(self, item):
attr = getattr(self.__connection, item)
@functools.wraps(attr)
def wrap(*args, **kwargs):
f = Future()
self.__io_loop.add_callback(self.__queue.put, (functools.partial(attr, *args, **kwargs), f))
return f
return wrap
示例3: Rx
# 需要导入模块: from tornado.queues import Queue [as 别名]
# 或者: from tornado.queues.Queue import empty [as 别名]
class Rx(PrettyPrintable):
def __init__(self, rx_tree, session_id, header_table=None, io_loop=None, service_name=None,
raw_headers=None, trace_id=None):
if header_table is None:
header_table = CocaineHeaders()
# If it's not the main thread
# and a current IOloop doesn't exist here,
# IOLoop.instance becomes self._io_loop
self._io_loop = io_loop or IOLoop.current()
self._queue = Queue()
self._done = False
self.session_id = session_id
self.service_name = service_name
self.rx_tree = rx_tree
self.default_protocol = detect_protocol_type(rx_tree)
self._headers = header_table
self._current_headers = self._headers.merge(raw_headers)
self.log = get_trace_adapter(log, trace_id)
@coroutine
def get(self, timeout=0, protocol=None):
if self._done and self._queue.empty():
raise ChokeEvent()
# to pull various service errors
if timeout <= 0:
item = yield self._queue.get()
else:
deadline = datetime.timedelta(seconds=timeout)
item = yield self._queue.get(deadline)
if isinstance(item, Exception):
raise item
if protocol is None:
protocol = self.default_protocol
name, payload, raw_headers = item
self._current_headers = self._headers.merge(raw_headers)
res = protocol(name, payload)
if isinstance(res, ProtocolError):
raise ServiceError(self.service_name, res.reason, res.code, res.category)
else:
raise Return(res)
def done(self):
self._done = True
def push(self, msg_type, payload, raw_headers):
dispatch = self.rx_tree.get(msg_type)
self.log.debug("dispatch %s %.300s", dispatch, payload)
if dispatch is None:
raise InvalidMessageType(self.service_name, CocaineErrno.INVALIDMESSAGETYPE,
"unexpected message type %s" % msg_type)
name, rx = dispatch
self.log.info(
"got message from `%s`: channel id: %s, type: %s",
self.service_name,
self.session_id,
name
)
self._queue.put_nowait((name, payload, raw_headers))
if rx == {}: # the last transition
self.done()
elif rx is not None: # not a recursive transition
self.rx_tree = rx
def error(self, err):
self._queue.put_nowait(err)
def closed(self):
return self._done
def _format(self):
return "name: %s, queue: %s, done: %s" % (self.service_name, self._queue, self._done)
@property
def headers(self):
return self._current_headers
示例4: Rx
# 需要导入模块: from tornado.queues import Queue [as 别名]
# 或者: from tornado.queues.Queue import empty [as 别名]
class Rx(PrettyPrintable):
def __init__(self, rx_tree, io_loop=None, servicename=None):
# If it's not the main thread
# and a current IOloop doesn't exist here,
# IOLoop.instance becomes self._io_loop
self._io_loop = io_loop or IOLoop.current()
self._queue = Queue()
self._done = False
self.servicename = servicename
self.rx_tree = rx_tree
self.default_protocol = detect_protocol_type(rx_tree)
@coroutine
def get(self, timeout=0, protocol=None):
if self._done and self._queue.empty():
raise ChokeEvent()
# to pull variuos service errors
if timeout <= 0 or timeout is None:
item = yield self._queue.get()
else:
deadline = datetime.timedelta(seconds=timeout)
item = yield self._queue.get(deadline)
if isinstance(item, Exception):
raise item
if protocol is None:
protocol = self.default_protocol
name, payload = item
res = protocol(name, payload)
if isinstance(res, ProtocolError):
raise ServiceError(self.servicename, res.reason,
res.code, res.category)
else:
raise Return(res)
def done(self):
self._done = True
def push(self, msg_type, payload):
dispatch = self.rx_tree.get(msg_type)
log.debug("dispatch %s %.300s", dispatch, payload)
if dispatch is None:
raise InvalidMessageType(self.servicename, CocaineErrno.INVALIDMESSAGETYPE,
"unexpected message type %s" % msg_type)
name, rx = dispatch
log.debug("name `%s` rx `%s`", name, rx)
self._queue.put_nowait((name, payload))
if rx == {}: # the last transition
self.done()
elif rx is not None: # not a recursive transition
self.rx_tree = rx
def error(self, err):
self._queue.put_nowait(err)
def closed(self):
return self._done
def _format(self):
return "name: %s, queue: %s, done: %s" % (
self.servicename, self._queue, self._done)