Embedding the server
A Python program can build the server itself with create_server() instead of
running linuxcnc-grpc. It returns a grpc.Server that is not started yet,
with its port already bound.
import signal
import sys
import time
from linuxcnc_grpc import create_server
shutdown_signals_received = []
unrecoverable_errors = []
def record_shutdown_signal(signal_number, frame):
shutdown_signals_received.append(signal_number)
for shutdown_signal in (signal.SIGINT, signal.SIGTERM):
signal.signal(shutdown_signal, record_shutdown_signal)
server = create_server(
host="127.0.0.1",
port=50051,
on_unrecoverable=unrecoverable_errors.append,
)
try:
server.start()
while not shutdown_signals_received and not unrecoverable_errors:
time.sleep(0.2)
finally:
server.stop(grace=5).wait(15)
sys.exit(1 if unrecoverable_errors else 0)This is what linuxcnc-grpc does. The signal handler only records, and the
main thread stops the server: a handler interrupts the main thread wherever
it is, and stopping the server from there can deadlock. A
normal LinuxCNC shutdown sends the process SIGTERM (see
When LinuxCNC stops), so the
program exits with status 0 then, and with status 1 when it has to be
restarted.
Stop with a grace period, as here. With stop(0) a health watcher may not
receive the final NOT_SERVING.
Pass host explicitly: the default is 0.0.0.0, every interface, and the
server has no authentication or TLS (see
Security considerations).
When the process must be restarted
linuxcnc-grpc exits with status 1 in four cases. A server built with
create_server() logs the same error and stays up, NOT_SERVING, until the
program stops it. In the first three, only a new process can connect to
LinuxCNC.
- The process can no longer connect to LinuxCNC (see Starting before LinuxCNC).
- The command helper could not be started 10 times in a row (same section).
- The LinuxCNC the server was attached to has shut down or been restarted (see When the server finds its LinuxCNC gone).
- LinuxCNC’s Python module has no
command.serial, so the server does not connect (see Requirements). When it is thehalmodule that is too old,create_server()raises instead.
Pass on_unrecoverable=callback to be told. The callback is called once, with
a description, for whichever case comes first. It runs on a background thread,
so have it only record the event, as above, and let the main thread stop the
server and exit.
The callback is not called once the server is being stopped, but the two can cross: a call can arrive at the moment the program stops the server, or just after. Ignore it once you have decided to stop.
The port is bound last
create_server() binds the port as its last step, so if it raises, no port is
held. Start the server it returned, or let the process end: stopping a gRPC
server that was never started does not release its port.
Other arguments
max_workers(default 32) sets the worker threads, and with them the limits on streams, waiting commands and health watchers (see Thread pool).command_helper=Falsesends commands from the server’s own process, like--in-process-commands. For what that costs, see A LinuxCNC task that does not respond.require_linuxcnc=Truemakescreate_server()raise when LinuxCNC is not running, like--require-linuxcnc.
Fork support
Importing linuxcnc_grpc sets GRPC_ENABLE_FORK_SUPPORT=false in the
process’s environment, unless the variable is already set. The server starts
child processes, and with gRPC’s fork support on, one of them can hang and
stall the server. The setting has no effect if grpc was imported first, so
import linuxcnc_grpc before grpc, or set the variable yourself.