I have a synchronization project, which invokes a python file to execute loads, so I initialize the application with nodejs. There is a node-cron that does the event every 1min, and I would like to know what is the best strategy to not jam or leave the python in memory, I would like to kill the open python as soon as its process is finished
my event.ts
import { ChildProcess } from 'child_process'
import { schedule } from 'node-cron'
import { PythonShell } from 'python-shell'
import util from 'util'
interface IDocumentEvent {
eventName: string
arquive: string
scriptPath: string
pythonOptions: [string]
mode: 'text'
}
export class EventManager {
public static timer: string = '0 */1 * * * *'
public static async running (input: IDocumentEvent) {
schedule(this.timer, () => {
console.log(util.inspect(process.memoryUsage()))
let python_process: ChildProcess
let processId = process.pid
const pyshell = new PythonShell(input.arquive,{
scriptPath: input.scriptPath,
pythonOptions: input.pythonOptions,
mode: input.mode
})
pyshell.send('Process started with ' + input.eventName.toUpperCase())
pyshell.on('message', (message: string) => {
console.log(input.eventName.toLocaleUpperCase() + ': ' + message);
})
pyshell.end(function (err, code, output) {
if (err) {
console.log(input.eventName.toLocaleUpperCase() + ': ' + err)
};
console.log('The exit code was: ' + code);
console.log('Clean condicoesResdepacho Process');
});
python_process = pyshell.childProcess
python_process.kill('SIGTERM')
python_process.kill('SIGINT')
}, {
scheduled: true,
timezone: 'America/Sao_Paulo'
})
}
}
I am using node-cron, and there are 4 events with it.