gpt4 book ai didi

javascript - 如何在node.js中杀死child_process——req.on close

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:55 25 4
gpt4 key购买 nike

我试图在调用“requst.on('close' ”时终止子进程。但是此回调中的 child.pid 始终指向最近的请求。问题是:如何将 child.pid 与请求匹配?谢谢

var query_script = "query.py"
var express = require('express');
var app = express();
var spawn = require('child_process').spawn;

app.get('/:version/query', function(request, response) {
child = spawn('python, ['query.py', request.originalUrl])
console.log("start pid=" + child.pid + ": " + request.originalUrl)

request.on('close', function () {
console.log("1: current pid=" + child.pid)
//child.kill('SIGTERM');
})

child.stdout.pipe(response)
});

app.listen(3000);
console.log('Listening on port 3000...');

最佳答案

通过将 var 放在闭包中定义 child 前面。

var child = spawn('python, ['query.py', request.originalUrl])

关于javascript - 如何在node.js中杀死child_process——req.on close,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com