mirror of
https://github.com/danog/parallel-functions.git
synced 2024-12-04 02:27:57 +01:00
54 lines
2.0 KiB
Markdown
54 lines
2.0 KiB
Markdown
|
---
|
||
|
title: Introduction
|
||
|
permalink: /
|
||
|
---
|
||
|
`amphp/parallel-functions` is a simplifying layer on top of `amphp/parallel`.
|
||
|
|
||
|
{:.warning}
|
||
|
> This library uses `jeremeamia/SuperClosure` to serialize closures, so its restrictions apply if you use closures. If serialization of a particular closure doesn't work, you can always write an autoloadable function and call that by name instead.
|
||
|
|
||
|
## Installation
|
||
|
|
||
|
This package can be installed as a [Composer](https://getcomposer.org/) dependency.
|
||
|
|
||
|
```bash
|
||
|
composer require amphp/parallel-functions
|
||
|
```
|
||
|
|
||
|
## Usage
|
||
|
|
||
|
Like all other `amphp` libraries, this library works in a fully asynchronous world. It returns promises as placeholders for future results of operations.
|
||
|
|
||
|
You don't need to know any details to use this library in traditional, fully synchronous applications. All you need is wrapping every function returning an `Amp\Promise` with `Amp\Promise\wait()`.
|
||
|
|
||
|
```php
|
||
|
<?php
|
||
|
|
||
|
use Amp\Promise;
|
||
|
use function Amp\ParallelFunctions\map;
|
||
|
|
||
|
$values = Promise\wait(map([1, 2, 3], function ($time) {
|
||
|
\sleep($time); // a blocking function call, might also do blocking I/O here
|
||
|
|
||
|
return $time * $time;
|
||
|
}));
|
||
|
```
|
||
|
|
||
|
### `parallel()`
|
||
|
|
||
|
`Amp\ParallelFunctions\parallel(callable): callable` wraps a `callable`, so it's executed in another thread / process on invocation. All arguments have to be serializable.
|
||
|
|
||
|
Currently this function only supports a direct string as function name or instances of `\Closure`. Support for other `callable` types might be added in the future.
|
||
|
|
||
|
### `map()`
|
||
|
|
||
|
`Amp\ParallelFunctions\map(array, callable): Promise` works similar to `array_map()`, but has a different signature. It accepts only one array instead of being variadic. It's thereby consistent with `Amp\ParallelFunctions\filter()`.
|
||
|
|
||
|
Restrictions of `Amp\ParallelFunctions\parallel()` apply.
|
||
|
|
||
|
### `filter()`
|
||
|
|
||
|
`Amp\ParallelFunctions\filter(array, callable, int): Promise` works like `array_filter()`, but returns a promise and executes in parallel.
|
||
|
|
||
|
Restrictions of `Amp\ParallelFunctions\parallel()` apply.
|