How to work with external child processes
The process builder (GtExternalProcessBuilder
) allows developers to create and spawn external child processes. It provides configuration api to pass arguments to the command or redirect standard streams (stdout
, stderr
and stdin
.
Asynchronous output
The following snippet creates an indefinitely running process that regularly pings the localhost
and configures the standard streams to be piped into a buffer:
process := GtExternalProcessBuilder new command: 'ping'; arg: '127.0.0.1'; pipeStdout; spawn.
The piped stdout
is buffered and can be polled at regular intervals. The code snippet below polls the stdout
as a string every 0.5 seconds
and prints it into a local transcript:
transcript := GtTranscript new. reader := process asynchronousStdout pollStringEvery: 0.5 second do: [ :aString | transcript show: aString ]. transcript
At wish, the running process can be killed, stopping the asynchronous polling of the output:
process kill
Synchronous output
If desired, a command can be executed synchronously blocking the current running process:
output := GtExternalProcessBuilder new command: 'date'; output. output stdout