The easiest way I know is to use the child_process package that comes with the node.
Then you can do something like:
const spawn = require("child_process").spawn; const pythonProcess = spawn('python',["path/to/script.py", arg1, arg2, ...]);
Then all you have to do is make sure you import sys into your python script, and then you can access arg1 with sys.argv[1] , arg2 with sys.argv[2] and so on .
To send data back to the node, just do the following in a python script:
print(dataToSendBack) sys.stdout.flush()
And then the node can listen to the data using:
pythonProcess.stdout.on('data', (data) => { // Do something with the data returned from python script });
Since this allows multiple arguments to be passed to the script using spawn, you can restructure the python script so that one of the arguments decides which function to call, and the other argument is passed to that function, etc.
Hope this was clear. Let me know if something needs to be clarified.
NeverForgetY2K May 04 '14 at 4:47 a.m. 2014-05-04 04:47
source share