Wednesday, July 22, 2020

PHP Parallel / Multithreading Example



Introduction

Parallel is a parallel concurrency extension for PHP 7.2+.

A brief description of the concepts core to parallel follows, more detailed information may be found within this section of the manual.

Runtime

A parallel\Runtime represents a PHP interpreter thread. A parallel\Runtime is configured with an optional bootstrap file passed to parallel\Runtime::__construct(), this is typically an autoloader, or some other preloading routine: The bootstrap file will be included before any task is executed.

After construction the parallel\Runtime remains available until it is closed, killed, or destroyed by the normal scoping rules of PHP objects. parallel\Runtime::run() allows the programmer to schedule tasks for execution in parallel. A parallel\Runtime has a FIFO schedule, tasks will be executed in the order that they are scheduled.

Function API

parallel implements a functional, higher level API on top of parallel\Runtime that provides a single function entry point to executing parallel code with automatic scheduling: parallel\run().

Task

A task is simply a Closure intended for parallel execution. The Closure may contain almost any instruction, including nested closures. However, there are some instructions that are prohibited in tasks:

  • yield
  • use by-reference
  • declare class
  • declare named function

** Note: Nested closures may yield or use by-reference, but must not contain class or named function declarations.

** Note: No instructions are prohibited in the files which the task may include.

Future

The parallel\Future is used to access the return value from the task, and exposes an API for cancellation of the task.

Channel

A task may be scheduled with arguments, use lexical scope variables (by-value), and return a value (via a parallel\Future), but these only allow uni-directional communication: They allow the programmer to send data into and retrieve data from a task, but do not allow bi-directional communication between tasks. The parallel\Channel API allows bi-directional communication between tasks, a parallel\Channel is a socket-like link between tasks that the programmer can use to send and receive data.

Events

The parallel\Events API implements a native feel (Traversable) event loop, and parallel\Events::poll() method. It allows the programmer to work with sets of channels and or futures. The programmer simply adds channels and futures to the event loop, optionally setting the input for writes with parallel\Events::setInput(), and enters into a foreach: parallel will read from and write to objects as they become available yielding parallel\Events\Event objects describing the operations that have occurred.

Requirements

  1. PHP 7.2+
  2. ZTS
  3. <pthread.h>

Installation

From sources :
git clone https://github.com/krakjoe/parallel.git
cd parallel
phpize
./configure --enable-parallel  [ --enable-parallel-coverage ] [ --enable-parallel-dev ]
make
make test
make install

From PECL :
pecl install parallel

Binary distributions :

Microsoft Windows: use zip archive from https://windows.php.net/downloads/pecl/releases/parallel/


Example

<?php
$runtime = new \parallel\Runtime;
$future  = $runtime->run(function(){
    return "World";
});
printf("Hello %s\n", $future->value());
?>

This may output something like (output abbreviated) :

Hello World



Reference :


Wednesday, April 22, 2020

PHP + PThreads (Posix Thread) Installation & Configuration Example


Installation is quite straightforward, although a little more involved than the Simple Windows Installation instructions suggest:
  1. Download a thread safe version of PHP, e.g. php-5.6.38-Win32-VC11-x86.
    • extract the .ZIP file to your hard drive, e.g. C:\php.
  2. Download the matching version of pthreads, e.g php_pthreads-2.0.10-5.6-ts-vc11-x86.
    • extract pthreadVC2.dll into the PHP folder, e.g. C:\php.
    • extract php_pthreads.dll into the extensions folder, e.g. C:\php\ext.
  3. In the PHP folder, copy either php.ini-development or php.ini-production to php.ini.
  4. Edit php.ini and add the line extension=php_pthreads.dll at the end of the file.
At this point pthreads should be working. You can verify this by opening a command prompt in the PHP folder and executing php --ri pthreads:
Screenshot of result
Note that the PHP and pthreads versions I referenced above require you to install the Visual Studio 2012 (VC11) runtime to function.

Edit: For completeness, I downloaded what seem to be the exact versions of PHP and pthreads you are using (the primary difference being x64):
Following the steps above I received the same result (except the pthreads version is reported as 2.0.9 of course).

Reference :