Search code examples
javascriptnode.jsmernchild-process

How to write a spawn child process in a an async function called by a GET/POST request


I am building a MERN stack application and want to connect my application to a python file which contains a machine learning algorithm that serves a functionality. Upon learning and using child processes created by a spawn function. I was able to retrieve data from the python code only when running a javascript file on its own but when copy pasting the code inside the GET request. The contents of the Buffer containing the string is always empty.

Here is my GET request:

const createSchedule = async (req, res) => {
    const { spawn } = require("child_process");
    const python = spawn('python', ["test2.py"]);
    const buffers = [];

    python.stdout.on('data', (chunk) => buffers.push(chunk));
    python.stdout.on('end', () => {
        console.log(buffers)
        if (buffers.length > 0) {
            const result = JSON.parse(Buffer.concat(buffers));
            console.log('Python process exited, result:', result);
            res.status(200).json(result)
        } else {
            console.log('Python process exited with no output');
            res.status(400).json({ error: "error" })
        }
    });

    let num_tas = 5
    let num_days = 6
    let num_slots = 5
    let num_courses = 3
    let num_tutorialGroups = 5

    // 5
    let taCourseAssignment = [
        [9, 6, 0],
        [3, 6, 0],
        [3, 0, 12],
        [9, 0, 0],
        [6, 0, 6]
    ]
    console.log(taCourseAssignment)

    // 6
    let taDayOffPreference = [
        [6, 5, 3, 4, 2, 1],
        [1, 2, 6, 5, 4, 3],
        [1, 2, 3, 4, 5, 6],
        [1, 2, 3, 6, 4, 5],
        [6, 1, 5, 4, 3, 2],
    ]

    // 7
    let sessionNumberPreference = [
        [2, 2, 2, 2, 2, 2],
        [2, 2, 2, 2, 2, 2],
        [0, 2, 2, 2, 2, 2],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
    ]

    // 8
    let schedule = [
        // sat
        [[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
        // sun
        [[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 1, 1], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0]],
        [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
        // mon
        [[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
        // tue
        [[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
        // wed
        [[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 1, 1], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0]],
        [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
        // thu
        [[[0, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 1, 1], [0, 0, 1, 0, 0], [0, 1, 0, 0, 0]],
        [[0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
        [[0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]],
        [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
        ],
    ]

    python.stdin.write(JSON.stringify([num_tas, num_days, num_slots, num_courses, num_tutorialGroups, taCourseAssignment, taDayOffPreference, sessionNumberPreference, schedule]));
    python.stdin.end()


}

Here is a screenshot of my file structure Image of file organization

I would like to send and receive data from the python file called model.py and store them to create objects. But fetching the data has completely failed . What is wrong with my code? I believe it might have to do with the function being async upon researching But I still can not figure out how to solve the issue.


Solution

  • Apparently is was a file path issue, the python file being in the same folder gave the error. Once I moved it out the code worked.