GLib.Subprocess에서 실행 파일을 실행하고 그 결과를 추출하려고합니다. 그러나 무한 루프 일 수 있으며 결코 끝나지 않습니다. 따라서 1 초 후에 하위 프로세스가 종료되기를 원합니다. 내가 시도한 것은 다음과 같습니다.
string executable = "/path/to/executable";
string input = "some_input";
uint timeout_id = 0;
...
...
try {
string output_string;
var subp = new GLib.Subprocess.newv ({executable}, SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDOUT_PIPE);
timeout_id = GLib.Timeout.add (1, () => {
subp.force_exit ();
source_remove ();
return false;
});
subp.communicate_utf8 (input, null, out output_string, null);
} catch (GLib.Error e) {
print ("Error: %s\n", e.message);
}
...
...
void source_remove () {
if (timeout_id > 0) {
Source.remove (timeout_id);
timeout_id = 0;
}
}
나는 또한 사용해 {"timeout", "1", executable}
보았지만 실행 파일이 무한 루프이면 멈추지 않습니다.
문제는 다음 줄에 있습니다.
subp.communicate_utf8 (input, null, out output_string, null);
GSubProcess.communicate_utf8()
프로세스가 종료 될 때까지 차단되는 동기 호출을 사용하고 있습니다 . 타임 아웃 콜백은 메인 루프 (동일한 스레드를 의미 함)에서도 호출되기 때문에 호출되지 않습니다. 이를 방지하려면 비동기 변형 인 GSubProcess.communicate_utf8_async()
.
참고 : source_remove()
타임 아웃 콜백 내 에서 호출 할 필요는 없습니다 . GSource는 false를 반환하여 이미 자동으로 제거됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다