【发布时间】:2022-01-01 09:51:11
【问题描述】:
点击前端的按钮后,我想执行一个 python 脚本,运行时间在 10 到 30 秒之间。
我正在尝试在我的发布路由/控制器中调用 python 脚本,但收到以下错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
在脚本运行之前,我不想向客户端发送任何内容。
路由/控制器:
const express = require("express");
const router = express.Router();
router.post("/solve", async function (req, res) {
const board = JSON.stringify({
board: req.body.grid,
});
const spawn = require("child_process").spawn;
const pythonProcess = spawn("python", ["./crossword/crossword.py", board]);
pythonProcess.stdout.on("data", (data) => {
// Do something with the data returned from python script
solved_data = JSON.parse(data.toString());
res.send(JSON.stringify(solved_data));
});
});
【问题讨论】:
标签: python node.js express asynchronous