/** * Sets the priority of a process. * * This allows for controlling the order of processes rather than using FIFO. * * Priority uses an ascending order where 0 > 1. * * Processes registered with a high priority will be executed before those with * a low or default priority. * * Process priority is handy when multiple process will execute and their order * is important. * * .. note:: * * This is different from an interrupt. * * Installed interrupts will still be executed before or after a * prioritized process. * * @param integer $priority Priority to assign * * @param callable|process $process PHP Callable or \XPSPL\Process. * * @return object Process * * @example * * Example #1 Basic Usage * * This installs multiple process each with a seperate ascending priority. * * .. code-block:: php * * <?php * * xp_signal(XP_SIG('foo'), priority(0, function(){ * echo 'foo'; * })); * * xp_signal(XP_SIG('foo'), priority(3, function(){ * echo 'bar'; * })); * * xp_signal(XP_SIG('foo'), priority(5, function(){ * echo 'hello'; * })); * * xp_signal(XP_SIG('foo'), priority(10, function(){ * echo 'world'; * })); * * The above code will output. * * .. code-block:: php * * foobarhelloworld */ function xp_priority($priority, $process) { if (!$process instanceof \XPSPL\Process) { $process = new \XPSPL\Process($process); } $process->set_priority($priority); return $process; }
/** * Enables a process to execute within it's own thread. * * .. warning:: * * Threaded functionality within XPSPL is *highly* experiemental... * * This has not been tested in a production environment. * * .. note:: * * To enable threads you must install and enable the PECL pthreads extension. * * Once installed threads will be automatically enabled. * * @example * * Example #1 Basic Usage * * Executing processes in their own thread. * * .. code-block:: php * * <?php * * xp_signal(XP_SIG('foo'), threaded_process(function($sig){ * print 'Executed in own thread'; * sleep(10); * }); * * @param callable $process PHP Callable * * @return void */ function xp_threaded_process($process) { if (!$process instanceof \XPSPL\Process) { $process = new \XPSPL\Process($process); } $process->enable_threads(); return $process; }
/** * Nullifies a processes exhaustion rate. * * .. note:: * * Once a process is registered with a null exhaust it will **never** * be purged from the processor unless a ``xp_flush`` is called. * * @param callable|process $process PHP Callable or Process. * * @return object Process * * @example * * Example #1 Basic Usage * * This example installs a null exhaust process which calls an awake signal * every 10 seconds creating an interval. * * .. code-block:: php * * <?php * import('time'); * * time\awake(10, xp_null_exhaust(function(){ * echo "10 seconds"; * })); */ function xp_null_exhaust($process) { if (!$process instanceof \XPSPL\Process) { $process = new \XPSPL\Process($process, null); return $process; } $process->set_exhaust(null); return $process; }
/** * Defines the number of times a process will execute when a signal is emitted. * * .. note:: * * By default all processes have an exhaust rate of null. * * @param callable|process $process PHP Callable or Process. * * @return object Process * * @example * * Example #1 Basic Usage * * Defines the given process with an exhaust of 5. * * .. code-block:: php * * <?php * * // Install a process for the foo signal that will execute up to 5 times. * xp_signal(XP_SIG('foo'), xp_exhaust(5, function(){ * echo 'foo'; * }); * * for($i=0;$i<10;$i++){ * xp_emit('foo'); * } * * The above code will output. * * .. code-block:: php * * foofoofoofoofoo * * @example * * Example #2 Creating a timeout * * .. code-block:: php * * <?php * * // Import the time module * xp_import('time'); * * time\awake(10, xp_exhaust(1, function(){ * echo 'This will execute only once.'; * }); * * The above code will output. * * .. code-block:: php * * This will execute only once. */ function xp_exhaust($limit, $process) { if (!$process instanceof \XPSPL\Process) { $process = new \XPSPL\Process($process, $limit); return $process; } $process->set_exhaust($limit); return $process; }
}, "process construction"); unittest\test(function ($test) { $process = new XPSPL\Process(function () { }); $test->false($process->is_exhausted()); $process->decrement_exhaust(); $test->false($process->is_exhausted()); $process = new XPSPL\Process(function () { }, 2); $process->decrement_exhaust(); $test->false($process->is_exhausted()); $process->decrement_exhaust(); $test->true($process->is_exhausted()); $process = new XPSPL\Process(function () { }, null); for ($i = 0; $i != 5; $i++) { $process->decrement_exhaust(); } $test->false($process->is_exhausted()); $process = new XPSPL\Process(function () { }, 0); $test->true($process->is_exhausted()); }, "Process exhaustion"); unittest\test(function ($test) { $process = new XPSPL\Process(function () { }); $process->set_priority(100); $test->equal(100, $process->get_priority()); $process->set_priority('a'); $test->equal($process->get_priority(), XPSPL_PROCESS_DEFAULT_PRIORITY); }, "Process Priority");