Пример #1
0
    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()
Пример #2
0
 def get(self, path):
     try:
         content = self.content(path)
         self.set_header('Content-Type', content.mime)
         if content.has_file() and os.name != 'nt':
             self.stream = PipeIOStream(content.open_fd())
             self.stream.read_until_close(callback=self.on_file_end,
                                          streaming_callback=self.on_chunk)
         else:
             self.finish(content.get_data())
     except NotAuthorizedError:
         self.write(exception_message())
         self.send_error(403)
     except FileNotFoundError:
         self.send_error(404)
     except:
         log.exception('error')
         self.send_error(500)
Пример #3
0
    def make_iostream_pair(self, **kwargs):
        r, w = os.pipe()

        return PipeIOStream(r, **kwargs), PipeIOStream(w, **kwargs)
Пример #4
0
 def _create_streams(self):
     read_fd, write_fd = os.pipe()
     write_stream = PipeIOStream(write_fd, io_loop=self.io_loop)
     read_stream = PipeIOStream(read_fd, io_loop=self.io_loop)
     return (write_stream, read_stream)
Пример #5
0
    def __init__(self,
                 command,
                 timeout=-1,
                 stdout_chunk_callback=None,
                 stderr_chunk_callback=None,
                 exit_process_callback=None,
                 stdin_bytes=None,
                 io_loop=None,
                 kill_on_timeout=False):
        """
        Initializes the subprocess with callbacks and timeout.

        :param command: command like ['java', '-jar', 'test.jar']
        :param timeout: timeout for subprocess to complete, if negative or zero then no timeout
        :param stdout_chunk_callback: callback(bytes_data_chuck_from_stdout)
        :param stderr_chunk_callback: callback(bytes_data_chuck_from_stderr)
        :param exit_process_callback: callback(exit_code, was_expired_by_timeout)
        :param stdin_bytes: bytes data to send to stdin
        :param io_loop: tornado io loop on None for current
        :param kill_on_timeout: kill(-9) or terminate(-15)?
        """
        self.aa_exit_process_callback = exit_process_callback
        self.aa_kill_on_timeout = kill_on_timeout
        stdin = Subprocess.STREAM if stdin_bytes else None
        stdout = Subprocess.STREAM if stdout_chunk_callback else None
        stderr = Subprocess.STREAM if stderr_chunk_callback else None

        Subprocess.__init__(self,
                            command,
                            stdin=stdin,
                            stdout=stdout,
                            stderr=stderr,
                            io_loop=io_loop,
                            shell=True)

        self.aa_process_expired = False
        self.aa_terminate_timeout = self.io_loop.call_later(
            timeout, self.aa_timeout_callback) if timeout > 0 else None

        self.set_exit_callback(self.aa_exit_callback)

        if stdin:
            self.stdin.write(stdin_bytes)
            self.stdin.close()

        if stdout:
            output_stream = PipeIOStream(self.stdout.fileno())

            def on_stdout_chunk(data):
                stdout_chunk_callback(data)
                if not output_stream.closed():
                    output_stream.read_bytes(102400, on_stdout_chunk, None,
                                             True)

            output_stream.read_bytes(102400, on_stdout_chunk, None, True)

        if stderr:
            stderr_stream = PipeIOStream(self.stderr.fileno())

            def on_stderr_chunk(data):
                stdout_chunk_callback(data)
                if not stderr_stream.closed():
                    stderr_stream.read_bytes(102400, on_stderr_chunk, None,
                                             True)

            stderr_stream.read_bytes(102400, on_stderr_chunk, None, True)