本文整理汇总了Python中pulsar.Future.done方法的典型用法代码示例。如果您正苦于以下问题:Python Future.done方法的具体用法?Python Future.done怎么用?Python Future.done使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pulsar.Future
的用法示例。
在下文中一共展示了Future.done方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_call_later
# 需要导入模块: from pulsar import Future [as 别名]
# 或者: from pulsar.Future import done [as 别名]
def test_call_later(self):
ioloop = get_event_loop()
tid = yield loop_thread_id(ioloop)
d = Future()
timeout1 = ioloop.call_later(
20, lambda: d.set_result(current_thread().ident))
timeout2 = ioloop.call_later(
10, lambda: d.set_result(current_thread().ident))
# lets wake the ioloop
self.assertTrue(has_callback(ioloop, timeout1))
self.assertTrue(has_callback(ioloop, timeout2))
timeout1.cancel()
timeout2.cancel()
self.assertTrue(timeout1._cancelled)
self.assertTrue(timeout2._cancelled)
timeout1 = ioloop.call_later(
0.1, lambda: d.set_result(current_thread().ident))
yield d
self.assertTrue(d.done())
self.assertEqual(d.result(), tid)
self.assertFalse(has_callback(ioloop, timeout1))
示例2: __init__
# 需要导入模块: from pulsar import Future [as 别名]
# 或者: from pulsar.Future import done [as 别名]
class StreamReader:
_expect_sent = None
_waiting = None
def __init__(self, headers, parser, transport=None):
self.headers = headers
self.parser = parser
self.transport = transport
self.buffer = b''
self.on_message_complete = Future()
def __repr__(self):
return repr(self.transport)
__str__ = __repr__
def done(self):
'''``True`` when the full HTTP message has been read.
'''
return self.on_message_complete.done()
def protocol(self):
version = self.parser.get_version()
return "HTTP/%s" % ".".join(('%s' % v for v in version))
def waiting_expect(self):
'''``True`` when the client is waiting for 100 Continue.
'''
if self._expect_sent is None:
if (not self.parser.is_message_complete() and
self.headers.has('expect', '100-continue')):
return True
self._expect_sent = ''
return False
def recv(self):
'''Read bytes in the buffer.
'''
if self.waiting_expect():
if self.parser.get_version() < (1, 1):
raise HttpException(status=417)
else:
msg = '%s 100 Continue\r\n\r\n' % self.protocol()
self._expect_sent = msg
self.transport.write(msg.encode(DEFAULT_CHARSET))
return self.parser.recv_body()
def read(self, maxbuf=None):
'''Return bytes in the buffer.
If the stream is not yet ready, return a :class:`asyncio.Future`
which results in the bytes read.
'''
if not self._waiting:
body = self.recv()
if self.done():
return self._getvalue(body, maxbuf)
else:
self._waiting = chain_future(
self.on_message_complete,
lambda r: self._getvalue(body, maxbuf))
return self._waiting
else:
return self._waiting
def fail(self):
if self.waiting_expect():
raise HttpException(status=417)
## INTERNALS
def _getvalue(self, body, maxbuf):
if self.buffer:
body = self.buffer + body
body = body + self.recv()
if maxbuf and len(body) > maxbuf:
body, self.buffer = body[:maxbuf], body[maxbuf:]
return body