PHP parallel programming tutorial and examples using php/parallel extension
- Comparison between single and multiple waiting queues (at post office...)
- PHP/parallel optimized run examples
- Step by step tutorial with exercises and solutions
-
Run a script e.g simulateQueue.php (may take much time on the first run since the Docker container is being build)
./run.sh -b simulateQueue/main.php
Command options :
- -b Builds container. This option MUST be used on first run ! It is optional afterwards.
- -i [official|frankenphp] Whether to use the offical PHP Docker image or that of FrankenPHP project. Defaults to "official"
- -d
<Docker-options>
Set specific Docker/run options. Useful to control CPU or memory use by Docker. Defaults to "--cpus=2.0".
simulateQueue
: compare single and multiple queues average and max waiting time. The queues are implemented as sequential programs. Multiple simulations ar run in parallel.lib/Pool.php
: a simple "Pool" class to run any number of tasks in parallel with a defined concurrency. Can be reused in any project !
Example are ordered by difficulty, starting with very easy examples.
Each examples/X (where X stands for the number of the example) can be run with the following command :
./run.sh -b tutorial/example/X/main.php
1
: "zzz !", Run two simple tasks in parallel2
: "Molière !" Run many tasks in parallel with arguments3
: "Alice is having a snap !", Future object and return value4
: "Alice and Bob sleep simultaneously", Get Future return value as soon as it completes5
: "multiple sleepers in limited number of rooms", Launch tasks in parallel in a limited number of threads and wait for them to complete6
: "http only !", Parallel download with Guzzle 77
: "SSH the PHP way ! ", Parallel SSH with SSH2 extension8
: "generic !", Parallel SSH as exec shell command and generic concurrency class9
: "Pong...zzz!", Run two simple tasks in parallel and synchronize them with a channel10
: Single queue parallel simulation11
: Multiple queues parallel simulation12
: Parallel thread launched from a parallel thread42
: Benchmark single and multiple queues in parallel100
: Data-flow : create an adder waiting from two parallel channels to make an addition101
: Prime sieve with daisy-chain filter processes compared to sequential algorithm102
: Fibonacci : Mimics the Go "select" statement with event-loop
- Get number of threads for your process
ps -o thcount <pid>
- Monitor context switches per second : watch "cs" value
vmstat 1