【发布时间】:2023-03-19 23:44:01
【问题描述】:
我要做的有点复杂。
我有一个 python 脚本,我想从 PHP 中运行它,但是在后台运行。我曾在某处看到,为了在后台运行 python 脚本,我必须使用 PHP 命令 exec(script.py) 来运行而无需等待返回:到目前为止没有问题。
第一个问题:
我必须用另一个 PHP 命令停止这个循环脚本,怎么做?
第二个问题:
我必须实现一个服务器端计时器,它会在时间结束时停止脚本。
我找到了这段代码:
<?php
$timer = 60*5; // seconds
$timestamp_file = 'end_timestamp.txt';
if(!file_exists($timestamp_file))
{
file_put_contents($timestamp_file, time()+$timer);
}
$end_timestamp = file_get_contents($timestamp_file);
$current_timestamp = time();
$difference = $end_timestamp - $current_timestamp;
if($difference <= 0)
{
echo 'time is up, BOOOOOOM';
// execute your function here
// reset timer by writing new timestamp into file
file_put_contents($timestamp_file, time()+$timer);
}
else
{
echo $difference.'s left...';
}
?>
来自this 的回答。
那么,有没有办法在 MySQL 数据库中实现呢? (与脚本停止集成没有问题)
【问题讨论】:
-
secure.php.net/manual/en/function.exec.php#89716 使用它来获取进程 ID,将其存储在文件或数据库中,然后稍后杀死具有该 ID 的进程。请记住,无论多么小,该进程可能已经停止,而另一个进程最终使用了该 ID(这意味着您正在杀死另一个进程)