https://github.com/pielonet/concurrency
PHP/parallel programming tutorial and examples
https://github.com/pielonet/concurrency
parallel-programming php
Last synced: 6 months ago
JSON representation
PHP/parallel programming tutorial and examples
- Host: GitHub
- URL: https://github.com/pielonet/concurrency
- Owner: pielonet
- License: gpl-3.0
- Created: 2021-11-21T16:06:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-13T07:48:58.000Z (8 months ago)
- Last Synced: 2024-11-08T01:18:59.233Z (8 months ago)
- Topics: parallel-programming, php
- Language: PHP
- Homepage:
- Size: 120 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# concurrency
PHP parallel programming tutorial and examples using [php/parallel](https://www.php.net/manual/en/book.parallel.php) extension- Comparison between single and multiple waiting queues (at post office...)
- PHP/parallel optimized run examples
- Step by step tutorial with exercises and solutions## How to run examples
- [Install Docker](https://docs.docker.com/engine/install/)
- Run a script e.g simulateQueue.php (may take much time on the first run since the Docker container is being build)
```bash
./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 ``
Set specific Docker/run options. Useful to control CPU or memory use by Docker. Defaults to "--cpus=2.0".### Scripts
- `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 !### Tutorial
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 :
```bash
./run.sh -b tutorial/example/X/main.php
```- `1` : "zzz !", Run two simple tasks in parallel
- `2` : "Molière !" Run many tasks in parallel with arguments
- `3` : "Alice is having a snap !", Future object and return value
- `4` : "Alice and Bob sleep simultaneously", Get Future return value as soon as it completes
- `5` : "multiple sleepers in limited number of rooms", Launch tasks in parallel in a limited number of threads and wait for them to complete
- `6` : "http only !", Parallel download with Guzzle 7
- `7` : "SSH the PHP way ! ", Parallel SSH with SSH2 extension
- `8` : "generic !", Parallel SSH as exec shell command and generic concurrency class
- `9` : "Pong...zzz!", Run two simple tasks in parallel and synchronize them with a channel
- `10` : Single queue parallel simulation
- `11` : Multiple queues parallel simulation
- `12` : Parallel thread launched from a parallel thread
- `42` : Benchmark single and multiple queues in parallel
- `100` : Data-flow : create an adder waiting from two parallel channels to make an addition
- `101` : Prime sieve with daisy-chain filter processes compared to sequential algorithm
- `102` : Fibonacci : Mimics the Go "select" statement with event-loop## Utils (Linux)
- Get number of threads for your process
```bash
ps -o thcount
```- Monitor context switches per second : watch "cs" value
```bash
vmstat 1
```## Links
- [PHP/parallel repository](https://github.com/krakjoe/parallel)
- [A more complex parallel example: a Web crawler by Joe Watkins aka Krakjoe](https://gist.github.com/krakjoe/0ee02b887288720d9b785c9f947f3a0a)## Fund php/parallel
[](https://gofund.me/c34f3dde)