PHP_Fork
[ class tree: PHP_Fork ] [ index: PHP_Fork ] [ all elements ]

Source for file exec_methods.php

Documentation is available at exec_methods.php

  1. <?php
  2. /**
  3.  * PHP_Fork class usage examples
  4.  * ==================================================================================
  5.  * NOTE: In real world you surely want to keep each class into
  6.  * a separate file, then include() it into your application.
  7.  * For this examples is more useful to keep all_code_into_one_file,
  8.  * so that each example shows a unique feature of the PHP_Fork framework.
  9.  * ==================================================================================
  10.  * exec_methods.php
  11.  *
  12.  * This example shows a workaround to execute methods into the child process.
  13.  * Always remember that this is an emulation, and the variable spaces are
  14.  * separated between separate processes!
  15.  * There're two kinds of methods: PHP_FORK_VOID_METHOD and PHP_FORK_RETURN_METHOD
  16.  * the first returns nothing, the second can return any serializable value
  17.  *
  18.  * ATTENTION: this feature of PHP_Fork is highly experimental;
  19.  * all things are OK until we run such an example, that does nothing and simply
  20.  * sleep() all time waiting for a call. Some experiement with real applications
  21.  * seems to show that firing the child process with a signal (that is part of the
  22.  * workaround...) causes the process to stop execution, and then to repeat ALL the
  23.  * run() method after signal caught... This is not an acceptable behaviour and
  24.  * should be tested better.
  25.  *
  26.  * ==================================================================================
  27.  *
  28.  */
  29.  
  30. // Import of base class
  31. require_once ("PHP/Fork.php");
  32.  
  33. // number of executeThreads we want
  34. define ("NUM_THREAD"2);
  35.  
  36. // this is needed as PHP 4.3 in order to use pcntl_signal()
  37. declare (ticks = 1);
  38.  
  39. /**
  40.  * Classes definition. In real world you surely want to keep each class into
  41.  * a separate file, then include() it into your application.
  42.  * For this examples is more useful to keep all_code_into_one_file
  43.  *
  44.  * executeThread class inherit from PHP_Fork and must redefine the run() method
  45.  * all the code contained into the run() method will be executed only by the child
  46.  * process; all other methods that you define will be accessible both to the parent
  47.  * and to the child (and will be executed into the relative process)
  48.  */
  49. class executeThread extends PHP_Fork {
  50.     function executeThread($name)
  51.     {
  52.         $this->PHP_Fork($name);
  53.         $GLOBALS["counter"= 0;
  54.     }
  55.  
  56.     function run()
  57.     {
  58.         while (true{
  59.             $GLOBALS["counter"]++;
  60.             sleep(1);
  61.         }
  62.     }
  63.     /**
  64.      * A simple method that can be called from the parent process;
  65.      * There are 2 types of methods, according to the return value
  66.      *
  67.      * PHP_FORK_VOID_METHOD is a method that return no value; a SINGLE array of parameters (or a single parameter) is expected to be passed
  68.      * PHP_FORK_RETURN_METHOD is a method that return an array to the calling process
  69.      *
  70.      * if nothing is specified, PHP_FORK_VOID_METHOD behaviour is the default
  71.      */
  72.     function setCounter($val)
  73.     {
  74.         if ($this->_isChild{
  75.             // do all your stuff here
  76.             // remember that only GLOBAL variables can be accessed with this trick, so
  77.             // if we need to change the value of the variable $counter from the parent into the
  78.             // child (like this code does...) we can't use a class variable ($this->counter),
  79.             // neither a local variable...
  80.             /**
  81.              * * START OF METHOD IMPLEMENTATION *
  82.              */
  83.             $GLOBALS["counter"$val[0];
  84.             /**
  85.              * * END OF METHOD IMPLEMENTATION *
  86.              */
  87.         }
  88.         /**
  89.          * Never change this line, it requires no adjustments...
  90.          */
  91.         else return $this->register_callback_func(func_get_args()__FUNCTION__);
  92.     }
  93.  
  94.     function getCounter($params)
  95.     {
  96.         if ($this->_isChild{
  97.             return $GLOBALS["counter"];
  98.         else return $this->register_callback_func(func_get_args()__FUNCTION__);
  99.     }
  100. }
  101.  
  102. /**
  103.  * Functions used by the console
  104.  */
  105. function _getInputCLI()
  106. {
  107.     $opt _read();
  108.     $opt strtoupper (trim($opt));
  109.     return $opt;
  110. }
  111.  
  112. function _read()
  113. {
  114.     $fp fopen("php://stdin""r");
  115.     $input fgets($fp255);
  116.     fclose($fp);
  117.  
  118.     return $input;
  119. }
  120.  
  121. /**
  122.  * Main program. Bring up two instances of the executeThread class that
  123.  * runs concurrently. It's a multi-thread app with a few lines of code!!!
  124.  * executeThread does nothing interesting, it simply has a counter and increment
  125.  * this counter each second... (see class definition at top of this file)
  126.  */
  127. for ($i = 0;$i NUM_THREAD;$i++{
  128.     $executeThread[$i&new executeThread ("executeThread-" $i);
  129.     $executeThread[$i]->start();
  130.     echo "Started " $executeThread[$i]->getName(" with PID " $executeThread[$i]->getPid("...\n";
  131. }
  132.  
  133. print "This is the main process.\nPress [X] to terminate, [S] to reset all counters, [G] to get the actual value of counters.\n";
  134.  
  135. /**
  136.  * Console simple listener
  137.  */
  138. while (true{
  139.     echo ">";
  140.  
  141.     $opt _getInputCLI();
  142.     echo "\n";
  143.     switch ($opt{
  144.         case "X":
  145.             // stops all threads
  146.             for ($i = 0;$i NUM_THREAD;$i++{
  147.                 $executeThread[$i]->stop();
  148.                 echo "Stopped " $executeThread[$i]->getName("\n";
  149.             }
  150.             exit;
  151.             break;
  152.  
  153.         case "S":
  154.             // setCounter is a PHP_FORK_VOID_METHOD
  155.             // it only need a paramenter (an array containing data that must be passed to child)
  156.             for ($i = 0;$i NUM_THREAD;$i++{
  157.                 $executeThread[$i]->setCounter(0);
  158.             }
  159.             break;
  160.  
  161.         case "G":
  162.             // getCounter is a method that reads the value of the thread counter
  163.             // so getCounter is a RETURN_METHOD and MUST be called with 2 parameters;
  164.             // the first is an array of data that must be passed to the method, the second is
  165.             // the constant (RETURN_METHOD)
  166.             for ($i = 0;$i NUM_THREAD;$i++{
  167.                 echo $executeThread[$i]->getName(" returns " $executeThread[$i]->getCounter(''PHP_FORK_RETURN_METHOD"\n";
  168.             }
  169.  
  170.             break;
  171.     }
  172. }
  173.  
  174. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.