Friday, 29 November 2019

Express + PythonShell - first request is always the slowest

Hi All,I`d like to raise a question in case somebody is familiar with this or has an idea.I`ve created an API using ExpressJS and for some POST routes, I`m using Python-Shell to call some scripts which takes the body from the POST request to do something with the data.A really weird thing that I am notificing is that whenever I am sending a request to these routes which call Python scripts (could be more than 1), the first request always takes the longest, then the more I use the route (send requests) the faster the reponse is received.It feels like the API is in an idle state, then 1st request comes in, it takes a while, then it`s getting used to getting calls to this route and behaves faster and faster. Latest test, 1st request takes 3 seconds, then 1s, then below 1s etc. etc.I`m thinking it might have something to do with the runtime of the scripts and spawning processes the call these scripts​Edit: inside the POST route, I have created a method which return a Promise with the results from the python script.Something likeexampleMethod : (json, pythonPath, pythonScript) => {return new Promise( (resolve, reject) => {let options = {mode: 'text',pythonPath: pythonPath,pythonOptions: ['-u'], // get print results in real-timeargs: [json] //json is data received in the body of the POST};// Here I call the Python scriptconst pyshell = PythonShell.run(globalPath + pythonScript, options, (err, results) => {if (err) {console.log('Error');console.log(err);reject(err);}});pyshell.stdout.on('data', function(data) {// console.log('Data: ',data);resolve(data);});pyshell.end();});}​Then in the POST request, I am calling the exampleMethod().then() to get async the results and continue the logic.​Many thanks!

Submitted November 29, 2019 at 11:57AM by Devtopia

No comments:

Post a Comment