【发布时间】:2022-08-19 06:17:51
【问题描述】:
我正在开发一个项目,在该项目中我需要运行一个基于 optapy 解决方案的 python 脚本。
因此,在运行 Web 服务之后,我期望得到 optapy 提供的解决方案作为响应。
但我刚刚收到这条线,它是在运行求解器后由 optapy 自动生成的。
16:28:03.158 [main ] INFO Solving started: time spent (186), best score (-45init/0hard/-2soft), environment mode (REPRODUCIBLE), move thread count (NONE), random (JDK with seed 0).
片刻之后,我在控制台中收到了这个错误
node:internal/errors:465
ErrorCaptureStackTrace(err);
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:372:5)
at ServerResponse.setHeader (node:_http_outgoing:576:11)
at ServerResponse.header (C:\\Users\\KaryGauss\\Desktop\\OSPlanner Service\\OSPlanner_NodeJS\\node_modules\\express\\lib\\response.js:794:10)
at ServerResponse.send (C:\\Users\\KaryGauss\\Desktop\\OSPlanner Service\\OSPlanner_NodeJS\\node_modules\\express\\lib\\response.js:174:12)
at Socket.<anonymous> (C:\\Users\\KaryGauss\\Desktop\\OSPlanner Service\\OSPlanner_NodeJS\\controllers\\test.js:10:13)
at Socket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at Pipe.onStreamRead (node:internal/stream_base_commons:190:23) {
code: \'ERR_HTTP_HEADERS_SENT\'
}
这是我调用脚本 python 的路线的控制器:
const spawn = require(\'child_process\').spawn;
const test = (req, res) => {
const py = spawn(process.env.PYTHON, [process.env.SCRIPT]);
py.stdout.on(\"data\", async (data) => {
//console.log(`stdout: ${data}`);
let allData = \"\";
allData += data;
// console.log(data.toString());
res.send(allData.toString());
});
py.stderr.on(\"data\", (data) => {
console.log(`stderr: ${data}`);
res.send(data);
});
}
module.exports = {
test
}
这是我的 Optapy 解决方案的主要功能
import sys
from domain import Reservation, ReservationSchedule, generate_problem
from constraints import define_constraints
import optapy.config
from optapy.types import Duration
from optapy import solver_factory_create
solver_config = optapy.config.solver.SolverConfig() \\
.withEntityClasses(Reservation) \\
.withSolutionClass(ReservationSchedule) \\
.withConstraintProviderClass(define_constraints) \\
.withTerminationSpentLimit(Duration.ofSeconds(30))
solver_factory = solver_factory_create(solver_config)
solver = solver_factory.buildSolver()
solution = solver.solve(generate_problem())
print(solution)
标签: node.js optaplanner optapy