Скрипт Pyhton зависает при вызове с помощью node.js child_process - PullRequest
0 голосов
/ 19 июня 2020

Я пытаюсь использовать обученный предсказатель модели в python в моем веб-приложении, используя node.js с помощью child_process, я могу запускать обычные команды, однако, когда я пытаюсь использовать pickle или joblib для загрузки моего обученного набора данных, сервер nodemon зависает.

Node-Js Код:

const express = require('express'); //used for server making
const ejs = require('ejs');//used to apply javascript into html code
const lodash = require('lodash');//used for text variations
const bodyParser = require('body-parser');//used to recives data from forms


const app=express()

app.set('view engine','ejs');


app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

app.get("/",(req,res)=>{
  res.render("home");
});

function callPythonCode(name){
  var spawn = require("child_process").spawn;
  var process = spawn('python',["AnimePredictor.py",name] );

  process.stdout.on('data',data=>{
    // res.send(data.toString())
    console.log(data.toString());
  });


};

app.post("/",(req,res)=>{
  const name = req.body.Name;
  callPythonCode(name);
})




app.listen(process.env.PORT ||3000,function(){
  console.log("server started at port 3000");
})

А это python код:

import pickle
import sys
import joblib

print("working")

# with open("public/animeTrain.pickle",'rb') as f:
#     anime,indices=pickle.load(f)

anime,indices = joblib.load("joblib.sav")
print("working2")

def get_index_from_name(name):
    return anime[anime["name"]==name].index.tolist()[0]

print("working3")


all_anime_names = list(anime.name.values)


def print_similar_animes(query=None,id=None):
    if id:
        for id in indices[id][1:]:
            print(anime.iloc[id]["name"])
    if query:
        found_id = get_index_from_name(query)
        for id in indices[found_id][1:]:
            print(anime.iloc[id]["name"])


print_similar_animes(sys.argv[1])

Я использую рассол, поэтому мне не нужно тренируйте его снова и снова, но будет оценена любая альтернатива.

...