Currently I have a Node.JS server that needs to run a python script. Part of this script imports some data from a server that takes time which is detrimental to the experience as this Python script needs to be ran frequently.
What I want is for the Node.JS server to run this Python script when the server is ran, then in the background constantly have it keep running, with the ability to (from the Node.JS server) call a python function that returns data.
So far I have this on the Node.JS server that runs a python script and outputs the response. This is repeated every time data is needed to be retrieved:
const util = require('util'); var options = { mode: 'text', args: [sentenceToUse] };
const readPy = util.promisify(PythonShell.run);
const test = await readPy("spacyTest.py", options);
var response = test.toString();
response = response.toString();
response = response.replace(/'/g, '"');
response = JSON.parse(response);
return(response);
console.log(test);
'''
How can I keep this running in the background without restarting the python script every time data is needed?
localhost, no need to hard-code any IP addresses. That would work for a socket.io connection or a web server connection. And, that will work no matter where you move it as long as they remain running on the same host. Since there are two built-in communication mechanisms already `stdio/stdout' and built-n intperprocess messaging, I would start with one of those until they aren't sufficient.