Pyjo.Server.Stdio - Standard input and output server

from Pyjo.Server.Stdio import Stdio

server = Stdio()


# Lines of the standard input in capitals
@server.on('stream')
def stream(server, stream):
    stream.on('read', lambda stream, chunk: stream.write(chunk.upper()))


server.run()

# Or import the module, as in Google Python Style Guide

import Pyjo.Server.Stdio

server = Pyjo.Server.Stdio.new()


# Lines of the standard input in capitals
@server.on('stream')
def stream(server, stream):
    stream.on('read', lambda stream, chunk: stream.write(chunk.upper()))


server.run()

Pyjo.Server.Stdio serves a protocol on the standard input and output of the process, as the transport stdio of MCP or LSP, or a protocol of lines that inetd or systemd serves, with the stream of Pyjo.IOLoop.Stdio, while the log goes to the standard error. It serves no HTTP: it builds no transaction, and the application gives the protocol with a subscriber of the event stream, which a plugin or the application subscribes in the hook before_server_start, so that the same application serves its routes with another server.

import Pyjo.IOLoop.Messages
import Pyjo.Server.Stdio

# The methods of JSON-RPC, one message on each line
@app.hook('before_server_start')
def stdio(server, app):
    if isinstance(server, Pyjo.Server.Stdio.object):
        server.on('stream', lambda server, stream: rpc.serve(Pyjo.IOLoop.Messages.new(stream)))

The server takes the standard input and output before it emits the hook, so that what the application and its plugins print while they start goes to the standard error, but not what a script prints while it is loaded, before its command runs.

The server runs the event loop until the stream closes, which it does once the standard input has ended and the rest is written, or which a subscriber of the event end of the stream does, as serve() does once the requests in progress have their responses.

The reactor watches pipes on POSIX systems, not on Windows, where the server raises an exception, as the stream does.

Signals

The Pyjo.Server.Stdio process can be controlled at runtime with the following signals.

INT, TERM

Shut down server gracefully: end the standard input, as at its end, and wait for the stream to be closed. A second INT, as of Ctrl-C pressed twice, shuts it down immediately, a second TERM does nothing.

QUIT

Shut down server immediately.

HUP

Ignored, as there is nothing to reload.

Events

Pyjo.Server.Stdio inherits all events from Pyjo.Server and can emit the following new ones.

stream

server.on('stream', lambda server, stream: ...)

Emitted when the Pyjo.IOLoop.Stdio stream of the standard input and output is ready to be served.

@server.on('stream')
def stream(server, stream):
    stream.on('end', lambda stream: stream.close_gracefully())

See also

Pyjoyment.Command.stdio, Pyjo.IOLoop.Stdio, Pyjo.IOLoop.Messages, Pyjo.Server.Daemon, Pyjoyment, https://mojolicious.org.

It has no counterpart in Mojolicious.

Classes

class Pyjo.Server.Stdio.Pyjo_Server_Stdio(*, ioloop: Pyjo_IOLoop | None = None, **kwargs: Any)[source]

Pyjo.Server.Stdio inherits all methods from Pyjo.Server and implements the following new ones.

ioloop: Pyjo_IOLoop
loop = server.ioloop
server.ioloop = Pyjo.IOLoop.new()

Event loop object to use for I/O operations, defaults to the global Pyjo.IOLoop singleton.

run() → None[source]
server.run()

Run server until the stream of the standard input and output closes, and wait for the signals INT, TERM and QUIT.