• <small id='VYr2Z'></small><noframes id='VYr2Z'>

    • <bdo id='VYr2Z'></bdo><ul id='VYr2Z'></ul>
    <tfoot id='VYr2Z'></tfoot>

    1. <legend id='VYr2Z'><style id='VYr2Z'><dir id='VYr2Z'><q id='VYr2Z'></q></dir></style></legend>
      <i id='VYr2Z'><tr id='VYr2Z'><dt id='VYr2Z'><q id='VYr2Z'><span id='VYr2Z'><b id='VYr2Z'><form id='VYr2Z'><ins id='VYr2Z'></ins><ul id='VYr2Z'></ul><sub id='VYr2Z'></sub></form><legend id='VYr2Z'></legend><bdo id='VYr2Z'><pre id='VYr2Z'><center id='VYr2Z'></center></pre></bdo></b><th id='VYr2Z'></th></span></q></dt></tr></i><div id='VYr2Z'><tfoot id='VYr2Z'></tfoot><dl id='VYr2Z'><fieldset id='VYr2Z'></fieldset></dl></div>

        PHP 守护进程/工作环境

        时间:2023-07-16
          • <bdo id='roIPx'></bdo><ul id='roIPx'></ul>

              <i id='roIPx'><tr id='roIPx'><dt id='roIPx'><q id='roIPx'><span id='roIPx'><b id='roIPx'><form id='roIPx'><ins id='roIPx'></ins><ul id='roIPx'></ul><sub id='roIPx'></sub></form><legend id='roIPx'></legend><bdo id='roIPx'><pre id='roIPx'><center id='roIPx'></center></pre></bdo></b><th id='roIPx'></th></span></q></dt></tr></i><div id='roIPx'><tfoot id='roIPx'></tfoot><dl id='roIPx'><fieldset id='roIPx'></fieldset></dl></div>
              <legend id='roIPx'><style id='roIPx'><dir id='roIPx'><q id='roIPx'></q></dir></style></legend>

              <small id='roIPx'></small><noframes id='roIPx'>

              <tfoot id='roIPx'></tfoot>
                    <tbody id='roIPx'></tbody>
                • 本文介绍了PHP 守护进程/工作环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  问题:我想实现几个 php-worker 进程,它们在 MQ 服务器队列上侦听异步作业.现在的问题是,简单地将这些进程作为服务器上的守护进程运行并不能真正让我对实例(负载、状态、锁定)进行任何级别的控制……除了转储 ps -aux.因此,我正在寻找某种运行时环境,让我可以在系统(进程)级别或更高层(某种 Java 风格的应用程序服务器)上监视和控制实例

                  Problem: I want to implement several php-worker processes who are listening on a MQ-server queue for asynchronous jobs. The problem now is that simply running this processes as daemons on a server doesn't really give me any level of control over the instances (Load, Status, locked up)...except maybe for dumping ps -aux. Because of that I'm looking for a runtime environment of some kind that lets me monitor and control the instances, either on system (process) level or on a higher layer (some kind of Java-style appserver)

                  有什么指点吗?

                  推荐答案

                  这里有一些可能有用的代码.

                  Here's some code that may be useful.

                  <?
                  define('WANT_PROCESSORS', 5);
                  define('PROCESSOR_EXECUTABLE', '/path/to/your/processor');
                  set_time_limit(0);
                  $cycles = 0;
                  $run = true;
                  $reload = false;
                  declare(ticks = 30);
                  
                  function signal_handler($signal) {
                      switch($signal) {
                      case SIGTERM :
                          global $run;
                          $run = false;
                          break;
                      case SIGHUP  :
                          global $reload;
                          $reload = true;
                          break;
                      }   
                  }
                  
                  pcntl_signal(SIGTERM, 'signal_handler');
                  pcntl_signal(SIGHUP, 'signal_handler');
                  
                  function spawn_processor() {
                      $pid = pcntl_fork();
                      if($pid) {
                          global $processors;
                          $processors[] = $pid;
                      } else {
                          if(posix_setsid() == -1)
                              die("Forked process could not detach from terminal
                  ");
                          fclose(stdin);
                          fclose(stdout);
                          fclose(stderr);
                          pcntl_exec(PROCESSOR_EXECUTABLE);
                          die('Failed to fork ' . PROCESSOR_EXECUTABLE . "
                  ");
                      }
                  }
                  
                  function spawn_processors() {
                      global $processors;
                      if($processors)
                          kill_processors();
                      $processors = array();
                      for($ix = 0; $ix < WANT_PROCESSORS; $ix++)
                          spawn_processor();
                  }
                  
                  function kill_processors() {
                      global $processors;
                      foreach($processors as $processor)
                          posix_kill($processor, SIGTERM);
                      foreach($processors as $processor)
                          pcntl_waitpid($processor);
                      unset($processors);
                  }
                  
                  function check_processors() {
                      global $processors;
                      $valid = array();
                      foreach($processors as $processor) {
                          pcntl_waitpid($processor, $status, WNOHANG);
                          if(posix_getsid($processor))
                              $valid[] = $processor;
                      }
                      $processors = $valid;
                      if(count($processors) > WANT_PROCESSORS) {
                          for($ix = count($processors) - 1; $ix >= WANT_PROCESSORS; $ix--)
                              posix_kill($processors[$ix], SIGTERM);
                          for($ix = count($processors) - 1; $ix >= WANT_PROCESSORS; $ix--)
                              pcntl_waitpid($processors[$ix]);
                      } elseif(count($processors) < WANT_PROCESSORS) {
                          for($ix = count($processors); $ix < WANT_PROCESSORS; $ix++)
                              spawn_processor();
                      }
                  }
                  
                  spawn_processors();
                  
                  while($run) {
                      $cycles++;
                      if($reload) {
                          $reload = false;
                          kill_processors();
                          spawn_processors();
                      } else {
                          check_processors();
                      }
                      usleep(150000);
                  }
                  kill_processors();
                  pcntl_wait();
                  ?>
                  

                  这篇关于PHP 守护进程/工作环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP 中的并行处理 - 你是怎么做的? 下一篇:我如何使用 PHP 实现这个场景?

                  相关文章

                • <legend id='PRmDg'><style id='PRmDg'><dir id='PRmDg'><q id='PRmDg'></q></dir></style></legend>

                    <i id='PRmDg'><tr id='PRmDg'><dt id='PRmDg'><q id='PRmDg'><span id='PRmDg'><b id='PRmDg'><form id='PRmDg'><ins id='PRmDg'></ins><ul id='PRmDg'></ul><sub id='PRmDg'></sub></form><legend id='PRmDg'></legend><bdo id='PRmDg'><pre id='PRmDg'><center id='PRmDg'></center></pre></bdo></b><th id='PRmDg'></th></span></q></dt></tr></i><div id='PRmDg'><tfoot id='PRmDg'></tfoot><dl id='PRmDg'><fieldset id='PRmDg'></fieldset></dl></div>

                        <bdo id='PRmDg'></bdo><ul id='PRmDg'></ul>

                      <small id='PRmDg'></small><noframes id='PRmDg'>

                    1. <tfoot id='PRmDg'></tfoot>