I am using Electron, Python and Flask to develop an app in Windows.
I use
require('child_process').spawn('python', [_script,port])
to start a child_process, but I cannot kill this child_process correctly.
When the electron app was closed, there was still a process called 'python' working in the background.
I tried almost every way, for instance p.kill(), or using taskkill.
Here is the python code:
from flask import Flask
import logiccode as mine
app = Flask(__name__)
@app.route('/hello')
def hello_world():
return mine.HelloWorld()
if __name__ == '__main__':
app.run(debug = True)
//create
pyProc = require('child_process').spawn('python', [_script,port])
if (pyProc != null) {
console.log('child process success\n')
console.log(pyProc.pid);
}
//kill
if(process.platform=='win32'){
require('child_process').spawn('taskkill',['/pid','/f','/t',pyProc.pid]);
}else{
pyProc.kill();
pyProc = null
pyPort = null
}
If there is some error in the code, please tell me how I can kill this child_process correctly.
I don't see any reason for the Windows-specific branch there, I assume you added it when pyProc.kill()
didn't work.
kill
defaults to SIGTERM. I'd expect that to work, but if you're seeing it not work, you can use the nuclear option of SIGKILL instead. As it says in signal(7)
:
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
So:
//kill
pyProc.kill('SIGKILL');
pyProc = null
pyPort = null
Hope it is not too late but I ran into the same problem, the reason why flask cannot be killed is because debug mode is set to True. Just call app.run() and node will kill flask.
if __name__ == '__main__':
app.run()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.