[英]multiprocessing: how to send data from parent to continuously running child process?
[英]How to send data from child process back to parent using Spawn in NodeJS
我正在尝试使用 spawn 子进程从 NodeJS 运行 python 脚本。 我能够成功地让 python 运行并使用控制台日志查看数据,但无法将实际数据发送回父函数以便我可以在其他地方使用它。
这是在 Node.js 中调用 spawn 的“Child.js”文件:
//Child.js: Node.js file that executes a python script using spawn...
function run_python_script(jsonString, callback){
//spawn new child process to call the python script
const {spawn} = require('child_process');
const python = spawn('python3', ["-c",`from apps import python_app; python_app.run(${jsonString});`]);
var dataToSend = ''
python.stdout.on('data', (data) => {
dataToSend = data.toString() //<---How do I return this back to other functions?
return callback(dataToSend) //<---this is not returning back to other functions.
})
python.stderr.on('data', (data)=>{
console.log(`stderr: ${data}`)
})
}
module.exports = { run_python_script };
================================================ ===================这是调用上面我想从 Python 接收数据的“Child.js”文件的文件...
const Child = require('./Child.js');
const callback = (x)=>{return x} //<--simply returns what's put in
let company_data = {
"industry": "Financial Services",
"revenue": 123456789
}
jsonString = JSON.stringify(company_data);
let result = ''
result = Child.run_python_script(jsonString, callback) //<-- I want the results from python to be stored here so I can use it elsewhere.
console.log(result) //<--this returns "undefined". Data isn't coming back
如果不需要异步生成 Python 进程,则可以使用spawnSync
而不是spawn
来直接获取包含其stdout
和stderr
作为字符串的对象:
const { spawnSync } = require('child_process');
const python = spawnSync('python3', ['-c', `print("hello"); assert False`], { encoding: "utf8" });
// Access stdout
console.log(python.stdout)
// Access stderr
console.log(python.stderr)
我想通了。 我将整个 spawn 例程包装在 Promise 中
// child.js
function run_python_script(jsonString){
const promise = new Promise((resolve, reject) => {
// spawn new child process to call the python script
const {spawn} = require('child_process');
const python = spawn('python3', ["-c",`from apps import python_app; python_app.run(${jsonString});`])
var dataToSend = ''
python.stdout.on('data', (data) => {
dataToSend = data.toString()
})
python.stderr.on('data', (data)=>{
console.log(`stderr: ${data}`)
reject(new Error(`stderr occured`))
})
python.on('close', (code)=>{
if (code !=0){
reject(new Error(`error code: ${code}`))
}else{
resolve(dataToSend)
}
})
})
return promise
}
module.exports = { run_python_script };
================================================ ==========================这里是调用上面子函数的父函数:
//Parent.js
const Child = require('./Child.js');
var data = {
"industry": "Financial Services",
"revenue": 12345678
}
var jsonString = JSON.stringify(data);
var result = ''
Child.run_python_script(jsonString).then(returnedData => {
var result = returnedData;
console.log(result)
})
.catch(err => {
console.log(err)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.