python-shell icon indicating copy to clipboard operation
python-shell copied to clipboard

Can't kill a script initiated via Node-Red function node

Open dubaleeiro opened this issue 7 years ago • 4 comments

Hello,

I can't kill a script initiated via Node-Red function node in the following flow:

[{"id":"10ab142a.8ba58c","type":"function","z":"14e30967.8aa527","name":"wave.py","func":"var PythonShell = context.global.PythonShell;\nvar python = new PythonShell('wave.py');\n\nvar options = {\n mode: 'text',\n pythonPath: '/usr/bin/python',\n pythonOptions: ['-u'], // get print results in real-time\n scriptPath: '/home/pi/ws2812-spi',\n //args: [10, 10, 10, 24]\n};\n\nif (msg.payload == 'start') {\n PythonShell.run('wave.py', options, function (err, results) {\n if (err) throw err;\n // results is an array consisting of messages collected during execution\n console.log('results: %j', results);\n});\n\n} else if (msg.payload == 'stop') {\n python.childProcess.kill();\n}","outputs":1,"noerr":0,"x":300,"y":520,"wires":[["128326d3.4467d9"]]},{"id":"7a86f3c9.c8c4ec","type":"inject","z":"14e30967.8aa527","name":"","topic":"","payload":"start","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":520,"wires":[["10ab142a.8ba58c"]]},{"id":"677bf035.569bd","type":"inject","z":"14e30967.8aa527","name":"","topic":"","payload":"stop","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":580,"wires":[["10ab142a.8ba58c"]]},{"id":"128326d3.4467d9","type":"debug","z":"14e30967.8aa527","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":520,"wires":[]}]

Any ideas please? Thanks!

dubaleeiro avatar Aug 26 '18 15:08 dubaleeiro

... and maybe to make it easier, thsi is the JS code in the function node:

`var PythonShell = context.global.PythonShell; var python = new PythonShell('wave.py');

var options = { mode: 'text', pythonPath: '/usr/bin/python', pythonOptions: ['-u'], // get print results in real-time scriptPath: '/home/pi/ws2812-spi', //args: [10, 10, 10, 24] };

if (msg.payload == 'start') { PythonShell.run('wave.py', options, function (err, results) { if (err) throw err; // results is an array consisting of messages collected during execution console.log('results: %j', results); });

} else if (msg.payload == 'stop') { python.childProcess.kill(); }`

dubaleeiro avatar Aug 26 '18 15:08 dubaleeiro

Try force killing. For example, take a look at the arepl-backend repo in my profile. In index.ts there is a function that force kills the python process.

Almenon avatar Aug 26 '18 16:08 Almenon

thanks a lot for the tip ! If I understood well, this instruction was usend in the arepl-backend repo, but it did not work either unfortunately... var python = new PythonShell('wave.py'); python.childProcess.kill('SIGKILL');

what I am doing wrong ...?

dubaleeiro avatar Aug 26 '18 16:08 dubaleeiro

I don't know, that looks right :/

Almenon avatar Jan 30 '19 05:01 Almenon