python-shell
python-shell copied to clipboard
Synchronized execution of python script
Is there way trun python script in sync with node
function() {
let pyshell = new PythonShell('.myscript.py')
let content = ""
pyshell.on('message',function(message){
content = message
})
return content // content needs to be message instead due to async nature it returns empty string
}
Look at the readme - you want the run command.
I am guessing you meant PythonShell.run() that one too is async function. It takes callback function.
Oh yeah you're right, sorry. We don't have a synchronous option but that's a good idea
If you are looking to add this feature , I will gladly help and I was looking to contribute and this is great opportunity for my first contribution I am familiar with node and related web technologies so can you guide me through what i need to know to implement synchronous option
Thanks! For sychronus operation first we need to migrate to using promises (read #77). I've created a v2 branch for this: https://github.com/extrabacon/python-shell/tree/version2
Once we are using promises then we can make sync versions of our functions - see this article: https://medium.com/@patarkf/synchronize-your-asynchronous-code-using-javascripts-async-await-5f3fa5b1366d
I want to ask don't it already come true
+1
re-posting my comment from #77:
To be honest I'm probably not going to get to this anytime soon. I'd be happy to review a PR if someone submits it. Or if someone wants to donate to charity I can use that as motivation to get around to implementing this.
We had the same issue today, and ended up promisifying it like so:
function someFunction(date, data){
return new Promise((resolve, reject) => {
let result;
let pyshell = new PythonShell('pyshell_test.py', {mode: 'text', args: [date]});
pyshell.send(JSON.stringify(data['someProperty']));
pyshell.on('message', function (message) {
result = JSON.parse(message);
});
pyshell.on('stderr', function (stderr) {
console.log(stderr);
});
pyshell.end(function (err, code, signal) {
if (err) reject(err);
console.log('The exit code was: ' + code);
console.log('The exit signal was: ' + signal);
console.log('finished');
resolve(result);
});
});
}
@nkhil this works (at least for now), thanks. :)
@nkhil implementation worked for me but I did something more like this in a Class:
callPythonScript = () => {
return new Promise((resolve, reject) => {
PythonShell.run(this.pythonScriptName, this.options, (err, result) => {
if (err) reject(err);
resolve(result);
});
});
};
@nkhil implementation worked for me but I did something more like this in a
Class:callPythonScript = () => { return new Promise((resolve, reject) => { PythonShell.run(this.pythonScriptName, this.options, (err, result) => { if (err) reject(err); resolve(result); }); }); };
One should use return reject(err) to avoid resolving the request afterwards. See https://stackoverflow.com/questions/32536049/do-i-need-to-return-after-early-resolve-reject