1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00
parallel/docs/index.md

49 lines
1.5 KiB
Markdown
Raw Normal View History

2017-12-09 00:53:12 +01:00
---
2018-11-04 16:59:50 +01:00
title: Parallel processing for PHP
2017-12-09 00:53:12 +01:00
permalink: /
---
2018-11-04 16:59:50 +01:00
This package provides *true parallel processing* for PHP using multiple processes or native threads, *without blocking and no extensions required*.
2017-12-09 00:53:12 +01:00
## Installation
This package can be installed as a [Composer](https://getcomposer.org/) dependency.
```bash
composer require amphp/parallel
```
## Usage
2018-11-04 16:59:50 +01:00
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';
use Amp\Parallel\Worker;
use Amp\Promise;
$urls = [
'https://secure.php.net',
'https://amphp.org',
'https://github.com',
];
$promises = [];
foreach ($urls as $url) {
$promises[$url] = Worker\enqueueCallable('file_get_contents', $url);
}
$responses = Promise\wait(Promise\all($promises));
foreach ($responses as $url => $response) {
\printf("Read %d bytes from %s\n", \strlen($response), $url);
}
```
[`file_get_contents`](https://secure.php.net/file_get_contents) 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 [Artax](https://amphp.org/artax/), our non-blocking HTTP client.
The functions you call must be predefined or autoloadable by Composer so they also exist in the worker processes.
2018-12-30 20:15:01 +01:00
Instead of simple callables, you can also [enqueue `Task` instances](./workers#task) with `Amp\Parallel\Worker\enqueue()`.