child_process 在 NodeJS 中应该没有错误

child_process in NodeJS not having an error when it's supposed to

我使用 nodeJS 和 express 作为服务器并使用 child_process.spawn 到 运行 一个 python 脚本。 运行 脚本后,我得到结果并将其作为 json 响应发送。

当我得到正确的 post 响应时,服务器响应正确的响应。 当我 post 错误的参数时,服务器响应错误消息(如预期的那样)

但是当我post在正确的参数post之后输入错误的参数时,它给出了之前post的响应,我不知道为什么。也许这是因为生成,我应该使用 exec 吗?

我想知道为什么会这样。 如果您需要更多详细信息,请告诉我。

const { json } = require('body-parser')
const express = require('express')
const { setTimeout } = require('timers')
const router = express.Router()
let dataToSend;



router.post('/',  (req, res) => {

    // console.log(req.body)

    let resol = req.body.Resolution;
    let url1 = req.body.FirstImageURI;
    let url2 = req.body.SecondImageURI;
    let roi = req.body.DetectionArea;
    let sens = req.body.Threshold;
    if (isNaN(sens)) sens = "50";

    console.log("start ai model");
    console.log(`params : ${url1} ${url2} ${roi} ${sens}`);
    const spawn = require("child_process").spawn;
    const pythonProcess = spawn('python', ["img_diff.py", url1, url2, roi, sens]);


    pythonProcess.stdout.on('data', function (data) {
        console.log('Pipe data from python script ...');
        dataToSend = data.toString();
        console.log(`data in stdout : ${dataToSend}`)
    });


    // in close event we are sure that stream from child process is closed
    pythonProcess.on('close', (code) => {
        try {
            jsonObject = {}
            console.log(`child process close all stdio with code ${code}`);
            // format json
            dtList = dataToSend.split('\n');
            ssim = Number(dtList[0].split(":")[1]);
            detected = "UnDetected"
            console.log(dtList[1])
            let addInfo = dtList[1].trim().slice(1, -1)
            addInfo = JSON.parse("[" + addInfo + "]");

            jsonObject.AdditionalInfo = []

            for (let i = 0; i < addInfo.length; i++) {
                let thisObject = {}
                thisObject.DetectedRegion = []
                for (let j = 0; j < addInfo[i].length; j++) {
                    coordObject = {}
                    coordObject.XCoord = addInfo[i][j][0]
                    coordObject.YCoord = addInfo[i][j][1]
                    thisObject.DetectedRegion.push(coordObject)
                }
                jsonObject.AdditionalInfo.push(thisObject)
            }

            if (jsonObject.AdditionalInfo.length > 0) {
                detected = "Detected"
            }
            jsonObject.Result = detected
            console.log(detected)
            res.json(jsonObject)
        } catch (e) {
            console.log(`error -> ${e}`)
            jsonObject.AdditionalInfo = [];
            let thisObject = {};
            thisObject.DetectedRegion = [];
            jsonObject.AdditionalInfo.push(thisObject);
            jsonObject.detected = "UnDetected";
            res.json(jsonObject);
        } 
    });

})



module.exports = router

仍然不知道为什么会这样, 但我的猜测是随着进程崩溃, Node只是给出了之前成功记忆的回应。

如果我有更好的答案会回来更新。