PHP pcntl 脚本 - 需要指导

发布于 2024-12-10 05:09:45 字数 1420 浏览 0 评论 0原文

我正在开发一个抓取项目,在应用程序的某个地方我需要此功能

一次又一次地运行脚本,并暂停几秒钟。

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毅然前行 2024-12-17 05:09:45

如果您只想每 x 秒重复一次 PHP 脚本,那么您可以使用 Fat Controller 为您处理所有脚本的守护进程和运行,以便您可以专注于 PHP 脚本中的业务逻辑。它是用 C 编写的,因此无论您在 PHP 脚本中做什么,它都非常稳定。

它基本上提供了以下功能:

  • 守护进程,因此您不必担心 nohup)
  • 多任务处理,在您的情况下不相关,因此您只需将并发进程设置为 1
  • 重复运行,因此您不必担心分叉。

有很多功能可以处理 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:

  • daemonising, so you don't have to mess about with nohup)
  • multitasking, in your case not relevant so you just set concurrent processes to 1
  • repeated running, so you don't have to mess about with forking.

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文