本文整理汇总了Python中tornado.iostream.PipeIOStream.write方法的典型用法代码示例。如果您正苦于以下问题:Python PipeIOStream.write方法的具体用法?Python PipeIOStream.write怎么用?Python PipeIOStream.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tornado.iostream.PipeIOStream
的用法示例。
在下文中一共展示了PipeIOStream.write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WriteFileHandler
# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import write [as 别名]
class WriteFileHandler(web.RequestHandler):
@web.asynchronous
def get(self):
print 'handler begin at %s' % datetime.now()
self.f = open('test.data', 'w')
self.stream = PipeIOStream(self.f.fileno())
self.stream.write(test_data, self.callback)
print 'handler async write at %s' % datetime.now()
def callback(self):
self.f.close()
self.finish()
print 'handler complete at %s' % datetime.now()
示例2: test_pipe_iostream_big_write
# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import write [as 别名]
def test_pipe_iostream_big_write(self):
r, w = os.pipe()
rs = PipeIOStream(r, io_loop=self.io_loop)
ws = PipeIOStream(w, io_loop=self.io_loop)
NUM_BYTES = 1048576
# Write 1MB of data, which should fill the buffer
ws.write(b"1" * NUM_BYTES)
rs.read_bytes(NUM_BYTES, self.stop)
data = self.wait()
self.assertEqual(data, b"1" * NUM_BYTES)
ws.close()
rs.close()
示例3: Shell
# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import write [as 别名]
class Shell(object):
def __init__(self, stdin=sys.stdin, stdout=sys.stdout, context: dict={}):
"""
Create a new shell.
:param stdin: file handle of the stdandard input
:param stdout: file handle of the standard output
:param context: exposed variables
"""
self.stdin = PipeIOStream(stdin.fileno())
self.stdout = PipeIOStream(stdout.fileno())
self.input_buffer = []
self.running = False
self.context = context
def start(self):
self.running = True
self.stdout.write(b"\r$>")
self.stdin.read_until(b'\n', self.on_line)
def on_line(self, chunk_bytes: bytes):
chunk = chunk_bytes.decode('utf-8', errors='ignore').rstrip('\n')
if not chunk.endswith('\\'):
self.input_buffer.append(chunk.strip())
line = " ".join(self.input_buffer)
self.input_buffer.clear()
self.on_command(line)
else:
self.input_buffer.append(chunk[:-1].strip())
self.stdout.write(b"\r ")
if self.running:
self.start()
@tornado.gen.engine
def on_command(self, command):
try:
if command:
code = compile(command + '\n', '<shell>', 'single')
res = eval(code, self.context)
if res is not None:
r = pprint.pformat(res).encode('utf-8')
yield tornado.gen.Task(self.stdout.write, r + b'\n')
except SystemExit:
raise
except:
yield tornado.gen.Task(self.stdout.write, traceback.format_exc().encode('utf-8'))
示例4: test_pipe_iostream
# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import write [as 别名]
def test_pipe_iostream(self):
r, w = os.pipe()
rs = PipeIOStream(r, io_loop=self.io_loop)
ws = PipeIOStream(w, io_loop=self.io_loop)
ws.write(b"hel")
ws.write(b"lo world")
rs.read_until(b" ", callback=self.stop)
data = self.wait()
self.assertEqual(data, b"hello ")
rs.read_bytes(3, self.stop)
data = self.wait()
self.assertEqual(data, b"wor")
ws.close()
rs.read_until_close(self.stop)
data = self.wait()
self.assertEqual(data, b"ld")
rs.close()
示例5: PipeStream
# 需要导入模块: from tornado.iostream import PipeIOStream [as 别名]
# 或者: from tornado.iostream.PipeIOStream import write [as 别名]
class PipeStream(Stream):
def __init__(self, rpipe, wpipe=None, auto_close=False):
"""Pipe-based stream
NOTE: reading from or writing to files, use os.open to get the file
descriptor instead of python's open. Socket file descriptors and
others are fine.
when you use os.pipe to generate one write pipe and one read pipe, you
need to pass both of them into init method.
:param rpipe: an integer file descriptor which supports read ops
:param wpipe: an integer file descriptor which supports write ops
:param auto: flag to indicate to close the stream automatically or not
"""
assert rpipe is not None
self._rpipe = rpipe
self._wpipe = wpipe
self._rs = PipeIOStream(self._rpipe) if self._rpipe is not None else None
self._ws = PipeIOStream(self._wpipe) if self._wpipe is not None else None
self.auto_close = auto_close
self.state = StreamState.init
self.exception = None
@tornado.gen.coroutine
def read(self):
if self.exception:
raise self.exception
if self.state == StreamState.completed or self._rpipe is None:
raise tornado.gen.Return("")
elif self.state == StreamState.init:
self.state = StreamState.streaming
chunk = ""
try:
chunk = yield self._rs.read_bytes(common.MAX_PAYLOAD_SIZE, partial=True)
except StreamClosedError:
# reach the end of the pipe stream
self.state = StreamState.completed
finally:
if self.exception:
raise self.exception
raise tornado.gen.Return(chunk)
@tornado.gen.coroutine
def write(self, chunk):
assert self._wpipe is not None
if self.exception:
raise self.exception
try:
yield self._ws.write(chunk)
self.state = StreamState.streaming
except StreamClosedError:
self.state = StreamState.completed
raise UnexpectedError("Stream has been closed.")
finally:
if self.exception:
raise self.exception
def set_exception(self, exception):
self.exception = exception
self.close()
def close(self):
self.state = StreamState.completed
if self._ws and self.auto_close:
self._ws.close()
if self._rs and self.auto_close:
self._rs.close()