PHP Concurrency
- Definitions
- Asynchronous code execution
- Multithread code execution
- Coroutines
- Distributed locking
- Inter-process communication
- PCNTL Signals
- Socket
- Shared memory
- Mutex, Semaphore
- Named pipe
- Expect - allows to interact with processes through PTY
- Process management
- Tools
- External program execution
- POSIX - process ids
- Stream
- PHP sources
- Repositories related to the PHP Language
- RFC
- Articles
Definitions
Concurrency: ability of different parts of a program to be executed out-of-order.
Parallelism: Form of parallel computing in which execution of processes are carried out concurrently across multiple processors in parallel computing environments.
Multitasking: is the concurrent execution of multiple tasks over a certain period of time. There are two types of multitasking:
- Co-operative, non-preemptive: is a style of computer multitasking when process voluntarily yield control and all programs must cooperate for the entire scheduling scheme to work.
- Asynchronous code execution
- Coroutines
- Preemptive: involves the use of an interrupt mechanism which suspends the currently executing process and invokes a scheduler to determine which process should execute next. Therefore, all processes will get some amount of CPU time at any given time.
- Threads
- Forks
Asynchronous code execution
Event loop extensions
Name | Extension | Source | Version | PHP version |
---|---|---|---|---|
ext-libevent | PHP Manual | PECL, git.php.net | 0.1.0 | >= 5.3.0, < 7.0.0 |
ext-event | PHP Manual | PECL, Bitbucket | 2.3.0 | >= 5.4, >= 7.0 |
ext-libev | GitHub manual | GitHub | < 7.0.0 | |
ext-ev | PHP Manual | PECL, BitBucket | >= 5.4, > 7.0 | |
eio | PHP Manual | PECL, GitHub | ||
swoole | GitHub manual | GitHub | ||
libuv | GitHub | PECL, GitHub | ||
concurent-php/ext-async | GitHub manual | GitHub | - | nightly |
ext-fiber | https://wiki.php.net/rfc/fibers, https://github.com/amphp/ext-fiber |
Links:
- Benchmarking libevent against libev - libev faster
ext-libevent
Has build-in OpenSSL library, non-blocking IO, http, dns.
pecl install libevent-0.1.0
ext-event
Event is a PECL extension providing interface to libevent C library.
The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.
Dockerfile: https://github.com/sokil/php-concurrency-labs/blob/master/docker/Dockerfile.ext-event
ext-libev
Library tries to improve libevent
. But this is only event library, instead of libevent giving non-blocking IO, http, etc.
Install libev
library:
sudo apt-get install libev-dev
Install php extension ext-libev
. Clone https://github.com/m4rw3r/php-libev and build extension:
phpize
./configure --with-libev
make
make install
libuv
Event loop frameworks
ReactPHP
Source: https://reactphp.org
Examples: https://github.com/sokil/php-concurrency-labs/tree/master/src/ReactPHP
cd src/ReactPHP
Docker build -t php-event .
docker run --rm -v `pwd`:/src php-event php /src/TimerExample.php
Articles
AMP
Site: https://amphp.org
Source: https://github.com/amphp
icicleio
Icicle is now deprecated in favor of Amp v2.0
Source: https://github.com/icicleio
Kraken
Source: http://kraken-php.com
Workerman
Source: https://github.com/walkor/Workerman
Multithread code execution
Thread extensions
Name | Source | Manual |
---|---|---|
Pthreads | GitHub,PECL | PHP Manual |
Pht | GitHub | PHP Manual |
Pthreads
Dockerfile: https://github.com/sokil/php-concurrency-labs/blob/master/docker/Dockerfile.ext-phtreads
Examples: https://github.com/sokil/php-concurrency-labs/tree/master/src/Pthreads
Coroutines
Coroutines are computer-program components that generalize subroutines for non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.
Articles
Distributed locking
Process management
Articles
Tools
- PHP-PM
- GitHub: https://github.com/php-pm/php-pm
- Spatie
- https://github.com/spatie/async
Tools
Stream
PHP Manual: http://php.net/manual/ru/book.stream.php
Examples: https://github.com/sokil/php-concurrency-labs/tree/master/src/Stream