Package: processcontroller
Fast StartInstall the package:pipinstall--userprocesscontrollerImport the main class:fromprocesscontrollerimportProcessControllerCreate a new instance:process_controller=ProcessController()The ProcessController() instanceMethodsThe run() method:Run a program (command and options detailed below)process_controller.run(command,options)The send() method:Send a string or bytearray to the subprogram stdin, you will usually use it inside a callback or in detached stateprocess_controller.send(str|bytes)The close() method:Close the stdin of the subprogram so it reaches EOF, you will usually use it inside a callback or in detached stateprocess_controller.close()The kill() method:Sends signal SIGKILL to the subprogram, the return_value should be (pid, 9) after that.
(this should not be so used to be honest)process_controller.kill()The wait() method:Used to synchronise the caller with the detached instance of ProcessController
Waits for the queue buffer to be handled and emptied.
If your process reads stdin such as bash or things like cat, be sure to call close() or you’ll end up with an infinite loop hereprocess_controller.wait()Attributesreturn_valueis the return value of the subprogram it will be
- (0, 0) if running
- (pid, status) if terminatedFunction ParametersProcessController.run()commandThe command parameter must be an array like:['/usr/bin/python','file.py']optionsCurrently, it supports the following keys, ‘when’, ‘input’, ‘echo’ and ‘detached’when:This key is used to listen to events occuring on the STDOUT of the subprogram
The value have to be an array of events
The “event” is in fact a match for some pattern:'when':[['^SomeRegex.*\n',callback],['^An other one.\n',cb]['^prompt>\$ $',cb]]Be careful about the ending line, the match will be called each time a char is added to the buffer, this helps matching prompts
maybe I’ll add an option to avoid such an expansive operation in the future
Every time a ‘n’ char is found, the line is treated once and reseted to ‘’The callbacks will be called with two arguments: the ProcessController instance, and the matched string:defcallback(processcontroller,string)You can automates user inputs in your callback when required by the subprogram using thesendfunction of your ProcessController instance:defcb(p,s):c.send('some input')input:This key is used to pre-fill the stdin of a subprogram before running it:pc=ProcessController()pc.run(['/bin/bash'],{'input':'echo test && exit'})You can set an array of input:'input':['one','two','three']# sends "one\ntwo\nthree\n"You can input str or bytes, conversion is handled for youecho:This key is a boolean that defaults to False.
When set to True, the ProcessController will print the input sent to your subprogram where it has been askeddetached:This key is used to make the program run in its own thread, making the call to run non-blocking:process_controller.wait()pc=ProcessController()pc.run(['/bin/bash'],{'detached':True})pc.send('echo test')# will print test to stdoutpc.close()# close stdin of subprogram, so that bash will read EOFprivate:This key is used to prevent the writing of your subprogram STDOUT on your main STDOUT,
One can still print what he wants with the help of a when event listener, indeed, the outputed lines will remain in the parameters of the callbacks functionsreadmode:Use this key to read the sub STDOUT char by char, or line by line (default)
It’s value can be ‘line’ or ‘char’
Useful to read prompts or anything that does not end with an EOLdecode:This option is used to tell processcontroller to decode or not what is read from the subprogram, True by defaultDon’t forget that you have to create a new instance a ProcessController everytime you want to execute a new subprogramPlease feel free to read the tests and code for a better understandingENJOY