//////////////////////////////////////////////////////////////////////////////////////////////////// // Helpers that print out some passed in text with some prefix // that will be included with every print out. ////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////// // Core Plugin that gets Python3 Version and Git Status on this plugin's folder. //////////////////////////////////////////////////////////////////////////////////////////////////// exportclassProcessTestextendsCoreService { staticdescriptor() { return { id:'snap.test.SubprocessExample', interfaces:CoreService. descriptor().interfaces, name:'Subprocess Example', description:'Run some sync and async subprocess.', dependencies: [] }; }
_subprocessPythonVersion() { // Store subprocess in `this` so we can kill it when the plugin closes this.pythonVersionSubprocess = sb.Subprocess.create('python3', ['--version'], {}); // Hook into subprocess constmyCommand = this.pythonVersionSubprocess.command; this.connections.push(this.pythonVersionSubprocess.started.connect(createStartedCallback(myCommand))); this.connections.push(this.pythonVersionSubprocess.stateChanged.connect(createStateChangedCallback(myCommand))); this.connections.push(this.pythonVersionSubprocess.errored.connect(createErrorCallback(myCommand))); this.connections.push(this.pythonVersionSubprocess.exited.connect(createExitCallback(myCommand))); this.connections.push(this.pythonVersionSubprocess.stdout.connect(createStdOutCallback(myCommand))); this.connections.push(this.pythonVersionSubprocess.stderr.connect(createStdErrCallback(myCommand)));
// Start the process this.pythonVersionSubprocess.start();
// Write to stdin for (leti = 0; i < 5; i++) { this.pythonVersionSubprocess.stdin.writeString('Hello, world: ' + i + '\n'); sb.spawnSync('sleep', ['1'], {}); } }
console.log("Start: subprocess for Git Status ------------------------------"); this._subprocessSyncGitStatus(); console.log("Done: subprocess for Git Status -------------------------------"); console.log("Start: subprocess for Python3 Version -------------------------"); this._subprocessPythonVersion(); }
stop() { // Need to kill the asynchronus process we started in `subprocessPythonVersion`. // For example when the app closes, or user disables the plugin. this.pythonVersionSubprocess.kill(); console.log("Done: subprocess for Python3 Version --------------------------"); } }
Before using anything in this namespace, make sure to import
LensStudio:Subprocess
and addsubprocess
in your plugin'smodule.json
.Example