本文整理汇总了Python中tornado.concurrent方法的典型用法代码示例。如果您正苦于以下问题:Python tornado.concurrent方法的具体用法?Python tornado.concurrent怎么用?Python tornado.concurrent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado
的用法示例。
在下文中一共展示了tornado.concurrent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import concurrent [as 别名]
def read(self):
def read_chunk(future):
if self.exception:
if self.exc_info:
future.set_exc_info(self.exc_info)
else:
future.set_exception(self.exception)
return future
chunk = b""
while len(self._stream) and len(chunk) < common.MAX_PAYLOAD_SIZE:
new_chunk = self._stream.popleft()
if six.PY3 and isinstance(new_chunk, str):
new_chunk = new_chunk.encode('utf8')
chunk += new_chunk
future.set_result(chunk)
return future
read_future = tornado.concurrent.Future()
# We're not ready yet
if self.state != StreamState.completed and not len(self._stream):
wait_future = self._condition.wait()
tornado.ioloop.IOLoop.current().add_future(
wait_future,
lambda f: f.exception() or read_chunk(read_future)
)
return read_future
return read_chunk(read_future)
示例2: write
# 需要导入模块: import tornado [as 别名]
# 或者: from tornado import concurrent [as 别名]
def write(self, chunk):
if self.exception:
raise self.exception
if self.state == StreamState.completed:
raise UnexpectedError("Stream has been closed.")
if chunk:
self._stream.append(chunk)
self._condition.notify()
# This needs to return a future to match the async interface.
r = tornado.concurrent.Future()
r.set_result(None)
return r