等待nodejs中python文件生成的响应



我已经使用nodejs编写了一个网站,并表示我的问题是我正在使用python脚本进行人脸识别。问题是,当我使用子进程从nodejs调用这个脚本时,识别需要时间(10到20秒,具体取决于(,而nodejs没有如果识别成功与否,则等待python文件发送的响应。这个回应对于我在网站上显示按钮是必要的,下一步你能给我一些指导吗。我应该怎么做才能得到想要的结果,或者你能指导我当识别是通过python脚本完成时,我应该如何向我的网站发出信号以进行进一步的处理吗。我的问题不清楚,请告诉我我会努力改进它。

index.js

const express = require("express");
app.get("/recognize/:id/:whom/:what", async (req, res) => {
const { id, whom, what } = req.params;
runPython(id, whom, what);
});

runPython.js

const { spawn } = require("child_process");
let dataFound = undefined;
const runPython = function (id, whom, what) {
const pythonProcess = spawn("python", ["fr_final.py", id, whom, what]);
pythonProcess.stdout.on("data", (data) => {
dataFound = data.toString();
console.log(dataFound);
// return resolve(resp);
});
//this line generate the required response which I want to use for further process
pythonProcess.on("exit", return dataFound);
};

open.js

const btnRecog = function (btn, lockerid, whom) {
btn.classList.remove("hidden");
btn.addEventListener("click", async function () {
// tell node to call python script for recognition and somehow wait for its response
const url = `http://localhost:8082/recognize/${lockerid}/${whom}/reco`;
const resp = await makeRequest(url);
if (!resp) return;
});
};
btnRecog(someBtn, LCR1800, banker) //when this is successfull then execute the other line
btnRecog(someOtherBtn, LCR1800, user) // only execute this after above line is done

我的应用程序基本上是一个digilocker,首先为银行家进行人脸识别,如果成功,则只为用户进行识别。我应该如何做到这一点。lockerid基本上是储物柜的id,必须对其进行识别

我们必须查看代码来确定如何调用python脚本以及如何发送响应。但请注意,Express也有一个连接超时属性。

Express将等待指定的时间让您的代码发送响应,否则它将使响应超时。如果您需要等待比默认时间更长的时间,请尝试增加超时值,或者使用HTTP池会话。

最新更新