gpt4 book ai didi

java - 使用超时处理程序从 PHP 调用 Java

转载 作者:行者123 更新时间:2023-12-02 07:37:17 25 4
gpt4 key购买 nike

问题:我正在通过 PHP 运行用户提交的 java 文件。 java 文件有可能导致无限循环。我该如何在php执行过程中处理这个问题?

这是我的代码:

$proc = proc_open($javaCmd, array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")), $pipes);
// Check status here logic ?
proc_close($proc);

它当前正在等待该过程完成,但我希望它在 30 秒或一分钟后超时。我试过set_time_limit(x) ,但这不会终止 java.exe

我的问题是,我是否可以在X秒后检查java.exe进程的状态,如果它仍在运行则终止?或者,我需要使用 timeout functionality在java中(即有一个主类在线程上执行用户提交的类)

最佳答案

是的,这是可能的。我不认为 java 进程在这方面与任何其他进程有什么不同。请参阅这些链接 unix exec with timeoutwindows exec with timeout .

我没有编写这段代码,但这里是复制粘贴的,以防原件从互联网上消失:

对于 UNIX:

<?php
function PsExecute($command, $timeout = 60, $sleep = 2) {
// First, execute the process, get the process ID

$pid = PsExec($command);

if( $pid === false )
return false;

$cur = 0;
// Second, loop for $timeout seconds checking if process is running
while( $cur < $timeout ) {
sleep($sleep);
$cur += $sleep;
// If process is no longer running, return true;

echo "\n ---- $cur ------ \n";

if( !PsExists($pid) )
return true; // Process must have exited, success!
}

// If process is still running after timeout, kill the process and return false
PsKill($pid);
return false;
}

function PsExec($commandJob) {

$command = $commandJob.' > /dev/null 2>&1 & echo $!';
exec($command ,$op);
$pid = (int)$op[0];

if($pid!="") return $pid;

return false;
}

function PsExists($pid) {

exec("ps ax | grep $pid 2>&1", $output);

while( list(,$row) = each($output) ) {

$row_array = explode(" ", $row);
$check_pid = $row_array[0];

if($pid == $check_pid) {
return true;
}

}

return false;
}

function PsKill($pid) {
exec("kill -9 $pid", $output);
}

对于 Windows:

<?php
// pstools.inc.php

function PsExecute($command, $timeout = 60, $sleep = 2) {
// First, execute the process, get the process ID
$pid = PsExec($command);

if( $pid === false )
return false;

$cur = 0;
// Second, loop for $timeout seconds checking if process is running
while( $cur < $timeout ) {
sleep($sleep);
$cur += $sleep;
// If process is no longer running, return true;
if( !PsExists($pid) )
return true; // Process must have exited, success!
}

// If process is still running after timeout, kill the process and return false
PsKill($pid);
return false;
}

function PsExec($command) {
exec( dirname(__FILE__). "\\psexec.exe -s -d $command 2>&1", $output);

while( list(,$row) = each($output) ) {
$found = stripos($row, 'with process ID ');
if( $found )
return substr($row, $found, strlen($row)-$found-strlen('with process ID ')-1); // chop off last character '.' from line
}

return false;
}

function PsExists($pid) {
exec( dirname(__FILE__). "\\pslist.exe $pid 2>&1", $output);

while( list(,$row) = each($output) ) {
$found = stristr($row, "process $pid was not found");
if( $found !== false )
return false;
}

return true;
}

function PsKill($pid) {
exec( dirname(__FILE__). "\\pskill.exe $pid", $output);
}

关于java - 使用超时处理程序从 PHP 调用 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com