node.js - Executing Python through NodeJS -


purpose:
execute python machine learning models existing nodejs api

so far:
i've written small class handle spawning , keeping child processes alive.
can spin up, execute, , return results python process, seems oddly slow. after profiling i've discovered < 18ms spent in python code, time calling execute() callback(result) takes > 500ms.

does end event take long time fire? suggestions helpful!

class:

const spawn = require('child_process').spawn;  class pythonrunner {     constructor(opts) {         this.py = {};         this.script = opts[0].script;         this.py.process = spawn('python', [opts[0].script]);     }       /*     time leak in execute between write , .on(data);     */      execute(args, callback) {         console.time('execute');         args = json.stringify(args);         let result = '';         this.py.process.stdout.on('data', (data) => {             // console.log(data.tostring());             result += data.tostring();         });         this.py.process.stdout.on('end', () => {             console.timeend('execute');             this.py.process = spawn('python', [this.script]);             callback(result)         });         this.py.process.stdin.write(`${args}`);         this.py.process.stdin.end();     } }  module.exports = pythonrunner; 

execution code:

let runner = require('../index');   let args = [8, 6, 22.92, 144.163, 3, 22.92, 498.562, 130.6713, -25.457, -26.1477, 22.92, 54, 101.166666666667, 4.52941176470588, 19.1995473684211, 23.81, -27.2147, 2.424, 2.472, 2.389] let options = [     { name: 'somepythonmodel', script: './__pycache__/some_python_model.cpython-36.pyc', args: args } ];   let script = new runner(options);  (function executeagain() {     script.execute(args, (res) => {         console.log(res);         executeagain();     }); })(); 

results time profiling: (read_in(), model_score(), , main() within python code)

execute: 538.514ms read_in(): 0.030994ms model_score(): 17.854929ms main(): 17.935991ms  execute: 539.759ms read_in(): 0.027895ms model_score(): 18.370152ms main(): 18.446922ms  execute: 545.662ms read_in(): 0.030994ms model_score(): 18.548965ms main(): 18.632889ms 


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -