Pyjo.IOLoop.Stdio - Standard input and output as a stream¶
from Pyjo.IOLoop.Stdio import Stdio
# Echo what comes in
stdio = Stdio()
stdio.on('read', lambda stdio, chunk: stdio.write(chunk))
# Or import the module, as in Google Python Style Guide
import Pyjo.IOLoop.Stdio
# Echo what comes in, and end once the input has ended and the rest is written
stdio = Pyjo.IOLoop.Stdio.new()
stdio.on('read', lambda stdio, chunk: stdio.write(chunk))
stdio.on('close', lambda stdio: stdio.reactor.stop())
stdio.start()
# Start reactor if necessary
if not stdio.reactor.is_running:
stdio.reactor.start()
Pyjo.IOLoop.Stdio is the standard input and output of the process as
one Pyjo.IOLoop.Stream, which reads the file descriptor 0 and
writes the file descriptor 1, as a connection of TCP is read and written,
so that a protocol does not tell them apart: the messages of a protocol, as
JSON-RPC or a protocol of lines, come in on the standard input and go out on
the standard output, and the log goes to the standard error.
The stream takes the standard input and output for itself while it is open:
it reads and writes duplicates of them, the file descriptor 0 reads
/dev/null, and the file descriptor 1 and sys.stdout write to
the standard error, so that a stray print, a library, or a process that
inherits them reads and writes nothing of the protocol. Closing the stream
gives them back.
A pipe or a socket is read and written in the non-blocking mode, which belongs
to the open file description that the process shares with its parent, so the
stream gives back the mode that it had when it closes; a terminal is read and
written without it. The reactor watches pipes, sockets and terminals, and a
file, or /dev/null, which a reactor of epoll does not watch, is read in
the next cycles of the event loop and written at once, as its reads and writes
do not block.
The stream has no inactivity timeout by default, as the peer of a server of standard input and output may send nothing for a long time.
The reactor watches pipes on POSIX systems, not on Windows, where the constructor raises an exception, and only one stream at a time has the standard input and output.
The end of a with block closes the stream.
Events¶
Pyjo.IOLoop.Stdio inherits all events from Pyjo.IOLoop.Stream
and can emit the following new ones.
end¶
stdio.on('end', lambda stdio: ...)
Emitted once the standard input has ended, while the standard output can still be written, as the responses of the requests that are in progress. Without a subscriber of this event, the stream closes gracefully, once all data has been written, as a connection of TCP that the peer closes; with one, the subscriber closes it.
stdio.on('end', lambda stdio: stdio.close_gracefully())
See also¶
Pyjo.IOLoop.Stream, Pyjo.IOLoop.ChildProcess, Pyjoyment,
https://mojolicious.org.
Inspired by process.stdin and process.stdout of Node.js, as
Mojolicious has no stream of the standard input and output.
Classes¶
- class Pyjo.IOLoop.Stdio.Pyjo_IOLoop_Stdio[source]¶
Pyjo.IOLoop.Stdioinherits all attributes and methods fromPyjo.IOLoop.Streamand implements the following new ones.- property bytes_waiting: int[source]¶
num = stream.bytes_waiting
Number of bytes that have been enqueued with
write()and are waiting to be written.
- close() None[source]¶
stdio.close()
Close stream immediately, and give back the standard input and output.
- end() None[source]¶
stdio.end()
End the standard input before its end, as at its end: stop reading it and emit the event
end, or close the stream gracefully without a subscriber, so that a server stops as when its peer ends.# Stop gracefully on a signal signal.signal(signal.SIGTERM, lambda signum, frame: stdio.reactor.next_tick(lambda reactor: stdio.end()))