I am developing an Electron application. In this application, I am spawning a Python process with a file's path as an argument, and the file itself is then passed to ffmpeg (through the ffmpeg-python module) and then goes through some Tensorflow functions.
I am trying to handle the case in which the user closes the Electron app while the whole background process is going. From my tests though, it seems like ffmpeg's process stays up no matter what. I'm on Windows and I'm looking at the task manager and I'm not sure what's going on: when closing the Electron app's window, sometimes ffmpeg.exe will be a single process, some other times it will stay in an Electron processes group.
I have noticed that if I kill Electron's process through closing the window, the python process will also close once ffmpeg has done its work, so I guess this is half-working. The problem is, ffmpeg is doing intensive stuff and if the user needs to close the window, then the ffmpeg process also NEEDS to be killed. But I can't achieve that in any way.
I have tried a couple things, so I'll paste some code:
main.js
// retrieve video data
ipcMain.handle('get-games', async (event, arg) => {
const spawn = require('child_process').spawn;
const pythonProcess = spawn('python', ["./backend/predict_games.py", arg]);
// sets pythonProcess as a global variable to be accessed when quitting the app
global.childProcess = pythonProcess;
return new Promise((resolve, reject) => {
let result = "";
pythonProcess.stdout.on('data', async (data) => {
data = String(data);
if (data.startsWith("{"))
result = JSON.parse(data);
});
pythonProcess.on('close', () => {
resolve(result);
})
pythonProcess.on('error', (err) => {
reject(err);
});
})
});
app.on('before-quit', function () {
global.childProcess.kill('SIGINT');
});
predict_games.py
(the ffmpeg part)
def convert_video_to_frames(fps, input_file):
# a few useful directories
local_dir = os.path.dirname(os.path.abspath(__file__))
snapshots_dir = fr"{local_dir}/snapshots/{input_file.stem}"
# creates snapshots folder if it doesn't exist
Path(snapshots_dir).mkdir(parents=True, exist_ok=True)
print(f"Processing: {Path(fr'{input_file}')}")
try:
(
ffmpeg.input(Path(input_file))
.filter("fps", fps=fps)
.output(f"{snapshots_dir}/%d.jpg", s="426x240", start_number=0)
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print("stdout:", e.stdout.decode("utf8"))
print("stderr:", e.stderr.decode("utf8"))
Does anyone have any clue?