Recycling Contexts

In line with our mantra, always consider employing the Worker model of execution.

The Worker model allows a context (Thread) to be reused, so if many tasks need to be executed asynchronously to the process (or creating Thread), but those tasks themselves are suitable to be executed synchronously then using a single context rather than many is a huge win.

Worker Brief

The patterns used to create Workers are much the same as those used to create Threads.

                class WebWorker extends Worker {
                    public function run(){}
                class WebRequest extends Threaded {
                    public $url;
                    public $data;
                    public function __construct($url) {
                        $this->url = $url;
                    public function run() {
                        $response = file_get_contents($this->url);
                        if ($response) {
                            /* process response */
                            $this->data = $response;
                $worker = new WebWorker();
                $work = array();
                /* create some random work */
                foreach (range(0, 10) as $index) {
                    /* retain reference to the work */
                    $work[$index] = new WebRequest(
                    /* stack the work for execution */
                /* shutting down waits for the execution of 
                    anything previously stacked, then joins the Worker */
                foreach ($work as $task) {
                    var_dump ($task->data);

In the example above, many requests are processed by the same context. The aim should be to always use Workers wherever possible, it is much more efficient to create one context (Worker), rather than many contexts (Threads).

Asynchronous Logging

A good example of using Workers in the real world might be an implementation of asynchronous logging.

In the real world, an asynchronous logger would be much more complex than the following example, for the purposes of a tutorial we will just write logs to standard output.

				class LogEntry extends Threaded {
					public $message;
					public $args;
					public function __construct($message, $args = null) {
						$this->message = $message;
						$this->args = $args;
					public function run() {
						/* for simplicity */
							"{$this->message}\n", $this->args);
				class Logger extends Worker {
					static $instance = null;
					static $work = [];
					public function __construct() {
					public static function log($message, $args = null) {
						if (self::$instance == null) {
							self::$instance = new Logger();
						$args = func_get_args();
						$wid = count(self::$work);
						self::$work[$wid]=new LogEntry(
					public function run() {}
				Logger::log("Hello %s", "World");
				Logger::log("Bye :)");

In the example above, it is the Worker that actually performs the writing of logs - however that is performed.