这个问题涉及到这个答案 https://stackoverflow.com/a/13402639/1125413我的其他问题之一。在这个答案中我被告知可以使用os.kill(pid, 0)
检查子进程是否已终止。如果它还在运行,None
被返回。如果它已终止,则会出现OSError
被提出。到目前为止,这一切都运行良好,但现在我处于这样的情况os.kill(pid, 0)
仍然返回None
尽管子进程已经终止。进程 ID 是 82430,在 OSX 上的活动监视器中我找不到具有该 ID 的进程。但是,当我打开 Python shell 并输入os.kill(82430, 0)
,那么它仍然返回None
。我不明白。
准确地说,我通过 Ajax GET 请求定期在 Django 视图中检查子流程的状态,如上所示。
服务器端的 Django 视图
def monitor_process(request):
response_data = {}
try:
os.kill(int(request.GET['process_id']), 0)
response_data['process_status'] = 'running'
except OSError:
response_data['process_status'] = 'finished'
return HttpResponse(json.dumps(response_data), mimetype='application/json')
客户端Ajax调用
var monitorProcess = function(processId) {
$.ajax({
dataType: 'json',
type: 'GET',
url: '/monitor_process/',
data: {'process_id': processId},
success: function(response) {
if (response.process_status == 'running') {
// Only this branch is always executed
// ...
}
else if (response.process_status == 'finished') {
// This branch never gets executed
// Stop checking for process status
// The periodic check was started by
// window.setInterval in a different Ajax call
clearInterval(monitorProcessId);
// ...
}
}
});
};
尽管该进程在某个时刻停止并消失在活动监视器中,os.kill()
显然不认识它。为什么会这样呢?非常感谢!