在 Nodejs 中运行 Python

问题描述 投票:0回答:1
fs.readFile('./db.json', function(err, data) {
let json = JSON.parse(data);

                    for (const i of json) {
                        if(i.filter == undefined) {
                            return;
                        } else {
                            exec('python', ['main.py', ${i.filter}, ${i.channel}, ${i.price}, ]);
                            console.log('started')
                        }
                    }
                })
    
    setInterval(() => {
    searchDb();
    }, 1000)
javascript python node.js child-process
1个回答
-1
投票

请至少花点时间创建一个更详细的问题。 这是你的答案。

const fs = require('fs');
const { exec } = require('child_process');

function searchDb() {
fs.readFile('./db.json', function(err, data) {
    if (err) {
        console.error('Error reading file:', err);
        return;
    }
    let json = JSON.parse(data);

    for (const i of json) {
        if (i.filter === undefined) {
            continue;
        } else {
            exec(`python main.py ${i.filter} ${i.channel} ${i.price}`, (error, stdout, stderr) => {
                if (error) {
                    console.error(`Execution error: ${error}`);
                    return;
                }
                console.log(stdout);
            });
            console.log('started');
            }
        }
    });
 }

setInterval(() => {
    searchDb();
}, 1000);
© www.soinside.com 2019 - 2024. All rights reserved.