parallel/README.md

88 lines
3.6 KiB
Markdown
Raw Normal View History

2017-11-29 18:27:30 -06:00
<p align="center">
2017-12-07 22:23:48 -06:00
<a href="https://amphp.org/parallel"><img src="https://raw.githubusercontent.com/amphp/logo/master/repos/parallel.png?v=12-07-2017" alt="parallel"/></a>
2017-11-29 18:27:30 -06:00
</p>
2015-07-10 15:15:42 -05:00
2017-11-29 18:27:30 -06:00
<p align="center">
2017-12-07 22:23:48 -06:00
<a href="https://travis-ci.org/amphp/parallel"><img src="https://img.shields.io/travis/amphp/parallel/master.svg?style=flat-square" alt="Build Status"/></a>
<a href="https://coveralls.io/github/amphp/parallel?branch=master"><img src="https://img.shields.io/coveralls/amphp/parallel/master.svg?style=flat-square" alt="Code Coverage"/></a>
<a href="https://github.com/amphp/parallel/releases"><img src="https://img.shields.io/github/release/amphp/parallel.svg?style=flat-square" alt="Release"/></a>
<a href="https://github.com/amphp/parallel/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" alt="License"/></a>
2017-11-29 18:27:30 -06:00
</p>
2017-07-17 22:59:56 -05:00
2018-11-04 16:59:50 +01:00
`amphp/parallel` provides *true parallel processing* for PHP using multiple processes or native threads, *without blocking and no extensions required*.
2015-09-27 23:45:56 -05:00
To be as flexible as possible, this library comes with a collection of non-blocking concurrency tools that can be used independently as needed, as well as an "opinionated" worker API that allows you to assign units of work to a pool of worker threads or processes.
2017-11-29 18:27:30 -06:00
## Installation
2017-11-29 18:27:30 -06:00
This package can be installed as a [Composer](https://getcomposer.org/) dependency.
2015-07-15 11:29:35 -05:00
2017-11-29 18:27:30 -06:00
```bash
2017-12-04 11:49:31 -06:00
composer require amphp/parallel
2017-11-29 18:27:30 -06:00
```
2015-09-27 23:45:56 -05:00
2018-11-04 16:59:50 +01:00
## Usage
The basic usage of this library is to submit blocking tasks to be executed by a worker pool in order to avoid blocking the main event loop.
```php
<?php
require __DIR__ . '/../vendor/autoload.php';
2021-12-01 10:28:40 +03:00
use Amp\Future;
2018-11-04 16:59:50 +01:00
use Amp\Parallel\Worker;
2022-02-06 23:39:55 +01:00
use function Amp\async;
2018-11-04 16:59:50 +01:00
$urls = [
'https://secure.php.net',
'https://amphp.org',
'https://github.com',
];
2021-12-01 10:28:40 +03:00
$futures = [];
2018-11-04 16:59:50 +01:00
foreach ($urls as $url) {
2022-02-06 23:39:55 +01:00
// FetchTask is just an example, you'll have to implement the Task interface for your task
$futures[$url] = async(fn () => Worker\submit(new FetchTask, $url));
2018-11-04 16:59:50 +01:00
}
2022-02-06 23:39:55 +01:00
$responses = Future\await($futures);
2018-11-04 16:59:50 +01:00
foreach ($responses as $url => $response) {
\printf("Read %d bytes from %s\n", \strlen($response), $url);
}
```
2022-02-06 23:39:55 +01:00
`FetchTask` is just used as an example for a blocking function here.
If you just want to fetch multiple HTTP resources concurrently, it's better to use [`amphp/http-client`](https://amphp.org/http-client/), our non-blocking HTTP client.
2015-09-27 23:45:56 -05:00
The functions you call must be predefined or autoloadable by Composer, so they also exist in the worker processes.
2015-09-27 23:45:56 -05:00
2017-11-29 18:27:30 -06:00
## Documentation
2015-09-27 23:45:56 -05:00
2017-12-02 13:48:18 +01:00
Documentation can be found on [amphp.org/parallel](https://amphp.org/parallel/) as well as in the [`./docs`](./docs) directory.
2017-11-29 18:27:30 -06:00
## Versioning
2015-09-27 23:45:56 -05:00
2017-12-04 11:49:31 -06:00
`amphp/parallel` follows the [semver](http://semver.org/) semantic versioning specification like all other `amphp` packages.
2015-09-27 23:45:56 -05:00
2017-11-29 18:27:30 -06:00
## Security
If you discover any security related issues, please email [`me@kelunik.com`](mailto:me@kelunik.com) instead of using the issue tracker.
## License
The MIT License (MIT). Please see [`LICENSE`](./LICENSE) for more information.
2017-11-29 18:27:30 -06:00
## Development and Contributing
2015-09-27 23:45:56 -05:00
Want to hack on the source? A [Vagrant](http://vagrantup.com) box is provided with the repository to give a common development environment for running concurrent threads and processes, and comes with a bunch of handy tools and scripts for testing and experimentation.
Starting up and logging into the virtual machine is as simple as
2018-11-04 16:59:50 +01:00
```bash
vagrant up && vagrant ssh
```
Once inside the VM, you can install PHP extensions with [Pickle](https://github.com/FriendsOfPHP/pickle), switch versions with `newphp VERSION`, and test for memory leaks with [Valgrind](http://valgrind.org).