/** * {@inheritdoc} */ public function run() { if ($this->runnable) { $this->runnable->run(); } return null; }
/** * Capture output * * @param lang.Runnable r * @param array<string, string> initial * @return array<string, string> */ public static function capture(Runnable $r, $initial = array()) { self::$streams = $initial; stream_wrapper_unregister('php'); stream_wrapper_register('php', __CLASS__); try { $r->run(); } catch (Exception $e) { } ensure($e); stream_wrapper_restore('php'); if ($e) { throw $e; } return self::$streams; }
static function run(Runnable $runnable) { $pid = pcntl_fork(); if ($pid == -1) { throw new \ErrorException('Inability to Launch Runnable'); } elseif ($pid) { // Parent return $pid; } else { //Child register_shutdown_function(function () { posix_kill(posix_getpid(), SIGHUP); }); $runnable->run(); exit(1); } }
use AppserverIo\Fhreads\Thread; class Runnable extends Thread { private $callables; public function __construct(callable ...$callables) { $this->callables = $callables; } public function run() { // add ref count to local function scope to avoid gc destroying object when modified $callables = $this->callables; do { $callable = array_shift($callables); $callable(); } while (count($callables) > 0); } } $runable = new Runnable(function () { echo 'Task 1.' . PHP_EOL; }, function () { echo 'Task 2.' . PHP_EOL; }, function () { echo 'Task 3.' . PHP_EOL; }, function () { echo 'Task 4.' . PHP_EOL; }, function () { echo 'Task 5.' . PHP_EOL; }); $runable->start(); $runable->join();