I have a web app with JavaScript front end and python back end. The user writes a phrase, clicks a button, python parses the sentence (natural language processing) and sends data back to the client. I use SpaCy for natural language processing. SpaCy takes a long time to load, so I wanted to know if I could pre-load SpaCy in NodeJS when I start the server (vs. importing SpaCy every time I spawn the python file - which is what I do now, See code below). Thanks!
JavaScript code:
io.on('connection', (socket) => {
socket.on('run_command_request', async (data) => {
let output_str = await run_python_command(data);
socket.emit('run_command_complete');
});
});
async function run_python_command(data) {
var spawn = require('child_process').spawn;
var py = spawn(python_executable, ['./run_command.py']); //PYTHON FILE CALLED EVERY TIME A USER CLICKS A BUTTON
var python_output_string ='';
py.stdin.write(JSON.stringify(data));
py.stdin.end();
return new Promise((res, rej) => {
py.stdout.on('end', function() {
res(python_output_string);
});
});
}
Python code:
import spacy ############# THIS STEP TAKES FOREVER #############
nlp = spacy.load("en_core_web_sm")
doc = nlp(tc)
verb = [token.lemma_ for token in doc if token.pos_ == "VERB"]
print(verb)
You could make your python program into a server. Your nodejs program could start it once when it starts. Then, instead of spawning each time, you just send it a request with your JSON data and wait for its response.
For simplicity, you might even make the python program an http server (on a known, local, non-publicly routed port) and you could just send it an http request whenever you want it to run the natural language parsing on a phrase.
Think of the python program more like a service that is running and available to handle requests anytime. That way you only pay the startup cost once.