I absolutely love using Node.JS for my web projects but i also love using Python at the same time, so i have been wondering, if is it possible to run Python scripts from Node, for example my Node.JS backend calls a python script to retrive some data from a SQL Database and gives it back to Node (i know i can do that all in Node but just using an example). I thought about doing it this way
1.Node creates a Json files which contains the variable the Python script will use(for example name:Jon birthDate:1996)
2.It runs a python script that reads those variables from that Json file(so it searches for Jon born in 1996)
3.It deletes the Json file when its done
4.Rinse and Repeat
Would this be a good and safe way of doing this type of thing or are there any other ways of running and "modifiying" the Python script?
I had to build a node app, but for the backend calculations, it seemed better to use NumPy and Pandas, so I did what you are doing... in the end I found an npm package called python-shell.
It works via promises, so I'd call my calculation script, and also would do some clean up with another Python script if there were any errors.
Worked pretty good. Code was something like this:
const PythonShell = require('python-shell');
router.get('/calc', (req, res) => {
PythonShell.run('py/calculate.py', {pyPath: pyPath , args: [tmpPath],}, function(err, results) {
if (err) {
PythonShell.run('py/clean_up.py', {pyPath: pyPath, args: [tmpPath2]}, function(err, results2) {
if (err) throw err;
res.json(
{
message: "error: Running clean up",
ang: 0,
vec: 0,
}
);
});
} else {
let data = JSON.parse(results);
let message = data[0];
let ang = data[1];
let vec = data[2];
res.json(
{
message: message,
ang: ang,
vec: vec,
}
);
}
});
});
Related
I want check in CAPL if the message is receiving or not in the simulation and if it is not coming in the trace, i want send new message. I have tried using functions like. I want to check particular message is receving or not?
TestWaitForPDU();TestWaitFormessage(msg,2000) etc but in simple configuration these are not working.
I have also tried using istimerActive() or istimerunning(), but these function will not check if message has stopped receiving or transmitting.
I am working in generic node.and i have tried something like this
on timer tslpack
{
int sleepack;
long Systemtime[9];
sleepack= isTimerActive(tslpack);
//write("Bus Active");
// write("Running Status %d",tslpack.isRunning());
if(sleepack==1)
{
write("timer cancelled");
cancelTimer(tslpack);
Settimer(tslpack,100);
}
else
{
result=1;
if(result ==1)
{
write("Bus Sleep");
sleeptime=timeNow();
result = 0;
}
}
You have mentioned that you are not writing the code in Test Node and you want to do it in Simulation Node. Clearly the functions TestWaitForPDU();TestWaitFormessage(msg,2000) are supposed to be used in tests as the name of the function shows.
I suppose you are waiting for a CAN message and so I am giving you a sample code for it.
variables
{
msTimer TimerToCheckMessage;
message CAN1.0x123 TxMsg; //Message which you want to send
}
on start
{
setTimer(TimerToCheckMessage,103);
TxMsg.dlc = 4;
}
on message CAN1.0x1 //Message which you want to check
{
setTimer(TimerToCheckMessage,103);
}
on timer TimerToCheckMessage
{
output(TxMsg);
setTimer(TimerToCheckMessage,103);
}
I am running a simple nodejs web application that executes a Python 2.7 script using the module python-shell. However, since this script takes too long to execute, around 3 minutes, the function fails with an ERR_EMPTY_RESPONSE error.
The function executes at the end, since Python still runs it in the background but the web application crashes.
generatePPage: (req, res) => {
const ps = require('python-shell');
let nombre = req.params.id;
var PythonShell = require('python-shell');
var options = {
pythonPath: '/usr/bin/python2.7',
args: [nombre],
};
var users = ps.PythonShell.run('./generateDetail.py', options, function (err, results) {
if (err){throw err;}
console.log('The results: %j', results);
res.render('success.ejs', {
title: 'Edit Project'
,project: ''
,message: 'Success'
});
});
},
How could I force it to wait longer?
Yes, you have to use setTimeout.
Check this link for more information: http://www.java2s.com/Tutorials/Javascript/Node.js_Tutorial/0270__Node.js_setTimeout_setInterval.htm
And also, check out this link: https://nodejs.org/ar/docs/guides/timers-in-node/
Solved it. The way to do it is to assign the return value of calling the listen function to a const and then assign a new timeout value in milliseconds.
const server = app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
server.timeout = 300000;
I have a Node.js application which is currently a web-based API. For one of my API functions, I make a call to a short Python script that I've written to achieve some extra functionality.
After reading up on communicating between Node and Python using the child_process module, I gave it a try and achieved my desired results. I call my Node function that takes in an email address, sends it to Python through std.in, my Python script performs the necessary external API call using the provided e-mail, and writes the output of the external API call to std.out and sends it back to my Node function.
Everything works properly until I fire off several requests consecutively. Despite Python correctly logging the changed e-mail address and also making the request to the external API with the updated e-mail address, after the first request I make to my API (returning the correct data), I keep receiving the same old data again and again.
My initial guess was that Python's input stream wasn't being flushed, but after testing the Python script I saw that I was correctly updating the e-mail address being received from Node and receiving the proper query results.
I think there's some underlying workings of the child_process module that I may not be understanding... since I'm fairly certain that the corresponding data is being correctly passed back and forth.
Below is the Node function:
exports.callPythonScript = (email)=>
{
let getPythonData = new Promise(function(success,fail){
const spawn = require('child_process').spawn;
const pythonProcess = spawn('python',['./util/emailage_query.py']);
pythonProcess.stdout.on('data', (data) =>{
let dataString = singleToDoubleQuote(data.toString());
let emailageResponse = JSON.parse(dataString);
success(emailageResponse);
})
pythonProcess.stdout.on('end', function(){
console.log("python script done");
})
pythonProcess.stderr.on('data', (data) => {
fail(data);
})
pythonProcess.stdin.write(email);
pythonProcess.stdin.end();
})
return getPythonData;
}
And here is the Python script:
import sys
from emailage.client import EmailageClient
def read_in():
lines = sys.stdin.readlines()
return lines[0]
def main():
client = EmailageClient('key','auth')
email = read_in()
json_response = client.query(email,user_email='authemail#mail.com')
print(json_response)
sys.stdout.flush()
if __name__ == '__main__':
main()
Again, upon making a single call to callPythonScript everything is returned perfectly. It is only upon making multiple calls that I'm stuck returning the same output over and over.
I'm hitting a wall here and any and all help would be appreciated. Thanks all!
I've used a Mutex lock for this kind of example. I can't seem to find the question the code comes from though, as I found it on SO when I had the same kind of issue:
class Lock {
constructor() {
this._locked = false;
this._waiting = [];
}
lock() {
const unlock = () => {
let nextResolve;
if (this._waiting.length > 0) {
nextResolve = this._waiting.pop(0);
nextResolve(unlock);
} else {
this._locked = false;
}
};
if (this._locked) {
return new Promise((resolve) => {
this._waiting.push(resolve);
});
} else {
this._locked = true;
return new Promise((resolve) => {
resolve(unlock);
});
}
}
}
module.exports = Lock;
Where I then call would implement it like this, with your code:
class Email {
constructor(Lock) {
this._lock = new Lock();
}
async callPythonScript(email) {
const unlock = await this._lock.lock();
let getPythonData = new Promise(function(success,fail){
const spawn = require('child_process').spawn;
const pythonProcess = spawn('python',['./util/emailage_query.py']);
pythonProcess.stdout.on('data', (data) =>{
let dataString = singleToDoubleQuote(data.toString());
let emailageResponse = JSON.parse(dataString);
success(emailageResponse);
})
pythonProcess.stdout.on('end', function(){
console.log("python script done");
})
pythonProcess.stderr.on('data', (data) => {
fail(data);
})
pythonProcess.stdin.write(email);
pythonProcess.stdin.end();
})
await unlock();
return getPythonData;
}
}
I haven't tested this code, and i've implemented where i'm dealing with arrays and each array value calling python... but this should at least give you a good start.
Hello I'm trying to interact with a python script inside a nodejs application at runtime.
The python script is more a command center for doing whatsapp operations called yowsup.
https://github.com/tgalal/yowsup/tree/master
I'm able to run the 'Yowsup Cli client' in a shell and work with it. But I want to run it in a nodejs application because it is written in python and I'm not good in python.
So what I did was to spawn the command I normally use in the shell like this:
var spawn = require('child_process').spawn,
ls = spawn('./yowsup/yowsup-cli', ['demos','--login', '49XXXXXXXXXXX:8bF0hUewVcX1hf6adpuasonFdEP=', '--yowsup', '-E', 's40']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
The problem is, that I don't get any data from the process. The python script normally prints some output as start but I can't get anything inside node while the process is running.
I looked inside the python script and saw that the output is generated like this:
print("%s send '%s'" % (messageProtocolEntity.getFrom(False), messageProtocolEntity.getBody()))
How can I get some data from the python script on runtime?
This is slightly different than your approach and uses an npm lib, but does work (results is the stdout of the random_geo.py script):
var py = require('python-shell');
var pyOptions = {
mode: 'text',
pythonPath: '/opt/local/bin/python',
scriptPath: '.'
};
function getCoords(req, res, next) {
py.run('random_geo.py', pyOptions, function(err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
}
});
}
I'm using python-shell to run a python script that classifies an image and returns a yes/no if that image is what I'm looking for or not.
Node.js code:
app.get('/classify', function(req, res) {
var options = {
mode: 'text',
pythonOptions: ['-u'],
scriptPath: 'src/',
args: ['--image', '../../images/image.png', 'classifer.xml']
};
PythonShell.run('classify_images.py', options, function(err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
res.send('classify');
});
If I run the command as a python script, it works fine but when I run it through node.js, it gives me back all no's.
The python command (in src) is:
python classify_images.py --image ../../images/image.png classifer.xml
Apparently I didn't put in the right file path - it worked after that.