英文:
How to write a spawn child process in a an async function called by a GET/POST request
问题
你的代码似乎有一些问题,导致从Python文件中获取数据时出现了问题。这可能与异步函数有关。你可以尝试以下步骤来解决问题:
-
确保Python文件(test2.py)中的机器学习算法正常工作,并且能够返回数据。
-
检查Python文件的路径是否正确。确保文件名的大小写和路径与你的项目文件结构一致。
-
确保你的Express应用已正确设置,以便可以处理GET请求并调用
createSchedule
函数。 -
在Python文件中,尝试使用
print
语句而不是console.log
来输出数据,以确保数据被正确发送到标准输出。 -
检查Python文件是否已正确处理输入数据。你可以在Python中使用
sys.stdin.read()
来读取JSON数据。 -
确保Python文件(test2.py)的依赖项已正确安装。
如果你仍然遇到问题,可以提供更多的错误消息或日志信息,以便更容易诊断问题。
英文:
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.
答案1
得分: 0
显然是一个文件路径问题,Python文件与相同文件夹中出现错误。将其移出后,代码正常运行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论