本文整理匯總了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