initial
This commit is contained in:
38
pages/api/spawn.js
Normal file
38
pages/api/spawn.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import { spawn } from "child_process";
|
||||
export default function (req, res) {
|
||||
console.log("spawnin");
|
||||
//console.log(req.body);
|
||||
//file
|
||||
let textData = req.body.profil;
|
||||
let replacedData = textData.replace("<22>", "o").replace("<22>", "e");
|
||||
console.log(replacedData);
|
||||
|
||||
var fs = require("fs");
|
||||
fs.writeFile("P.txt", replacedData, function (err) {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
}
|
||||
});
|
||||
|
||||
//py
|
||||
|
||||
const python = spawn("python3", ["a.py"]);
|
||||
|
||||
let dataToSend;
|
||||
python.stdout.on("data", function (data) {
|
||||
console.log("Pipe data from python script ...");
|
||||
dataToSend = data.toString();
|
||||
//console.log(dataToSend)
|
||||
});
|
||||
python.stderr.on('data', (data) => {
|
||||
console.error(`stderr: ${data}`);
|
||||
});
|
||||
// in close event we are sure that stream from child process is closed
|
||||
python.on("close", (code) => {
|
||||
console.log(`child process close all stdio with code ${code}`);
|
||||
// send data to browser
|
||||
console.log(dataToSend);
|
||||
console.log("done");
|
||||
res.send("dataToSend");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user