I am currently working on a project which requires the user to click on an 'execute' button to call a child_process in the backend for executing a time-consuming task. Codes are as follows,
let child = child_process.spawn(//args);
let data = '';
let err = '';
for await(const chunk of child.stdout) {
data += chunk;
}
for await(const chunk of child.stderr) {
err += chunk;
}
return {"out": data, "err": err};
Decoding is omitted here. The task of the child_process would output all results at the end of the process so there would be no output during the task is ongoing.
And the user interface has a button for the user to cancel the task while it is executing. I have tried if (cancelFlag) {child.kill('SIGTERM')}
right after the child_process.spawn
, but it would not work. I suppose a listener is needed for the child_process?
Great thanks for any suggestions.