PHP pcntl 脚本 - 需要指导
我正在开发一个抓取项目,在应用程序的某个地方我需要此功能
一次又一次地运行脚本,并暂停几秒钟。
我尝试使用 pcntl 来完成这项工作。所以写了这个脚本,
/************************/
$intra_sleep=10; // we're going to set the intra process launch sleep at 10 seconds
$task_process=null; // by default this is set to null -- do nothing
$loop_limit=0; // this is the number of times the loop shoul run -- if set to -1 look infinite number of times
if (isset($argv[1])) $task_process=$argv[1];
if (isset($argv[2])) $intra_sleep=$argv[2];
if (isset($argv[3])) $loop_limit=$argv[3];
for ($loop_count=0; $loop_limit==-1 ? true : $loop_count< $loop_limit; $loop_count++)
{
$pid= pcntl_fork();
if ($pid == -1)
{
die('MASTER: could not fork');
}
else if ($pid==0)
{
if ($task_process)
{
echo "Sleeping for $intra_sleep Seconds\n";
sleep($intra_sleep);
echo "Launching Child \n\n";
exec($task_process); // from here process script is being launched
}
else
{
echo " CLONE: no task process defined -- doing nothing " . PHP_EOL;
}
}
else
{
pcntl_waitpid($pid,$status);
}
}
/*********************/
我从 CLI 调用这个脚本,就像这样
nohup php /this/script.php "php /path/to/process.php" 10 -1
我希望 process.php 将以 10 秒的间隔一次又一次地启动。它正在按照我的预期进行工作,但是当我检查正在运行的进程时,有数千个进程正在运行,由该脚本启动。
我的要求非常简单:脚本应该一次又一次地启动并暂停 10 秒。
I am working on a scraping project, somewhere in the application I need this functionality
run a script again and again with a pause of some seconds.
I tried to use pcntl to accomplish the job. so wrote this script
/************************/
$intra_sleep=10; // we're going to set the intra process launch sleep at 10 seconds
$task_process=null; // by default this is set to null -- do nothing
$loop_limit=0; // this is the number of times the loop shoul run -- if set to -1 look infinite number of times
if (isset($argv[1])) $task_process=$argv[1];
if (isset($argv[2])) $intra_sleep=$argv[2];
if (isset($argv[3])) $loop_limit=$argv[3];
for ($loop_count=0; $loop_limit==-1 ? true : $loop_count< $loop_limit; $loop_count++)
{
$pid= pcntl_fork();
if ($pid == -1)
{
die('MASTER: could not fork');
}
else if ($pid==0)
{
if ($task_process)
{
echo "Sleeping for $intra_sleep Seconds\n";
sleep($intra_sleep);
echo "Launching Child \n\n";
exec($task_process); // from here process script is being launched
}
else
{
echo " CLONE: no task process defined -- doing nothing " . PHP_EOL;
}
}
else
{
pcntl_waitpid($pid,$status);
}
}
/*********************/
I am calling this script from CLI like this way
nohup php /this/script.php "php /path/to/process.php" 10 -1
I expect that process.php will be launched again and again with 10 seconds intervals. It was doing work what I was expecting, but when I check runing processes, there were thousands processed running, launched by this script.
My requirement is very simple: a script should be launched again and again with 10 second Pause.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想每 x 秒重复一次 PHP 脚本,那么您可以使用 Fat Controller 为您处理所有脚本的守护进程和运行,以便您可以专注于 PHP 脚本中的业务逻辑。它是用 C 编写的,因此无论您在 PHP 脚本中做什么,它都非常稳定。
它基本上提供了以下功能:
有很多功能可以处理 PHP 脚本中的错误和错误的长时间运行脚本。
它的安装和设置非常简单,网站上有大量文档可以帮助您入门。值得快速浏览一下,至少看看它是否会对您有帮助。
http://fat-controller.sourceforge.net/
If you just want to repeat a PHP script every x seconds, then you could use The Fat Controller to handle all the daemonising and running of scripts for you so you can concentrate on business logicin your PHP script. It's written in C so it's very stable, no matter what you do in your PHP scripts.
It basically provides the following functionality:
There's lots of functionality to handle errors in PHP scripts and erroneous long running scripts.
It's pretty easy to install and get set up and there's plenty of documentation on the website to get you started. It's worth a quick look at least to see if it will help you.
http://fat-controller.sourceforge.net/