Skip to content

Commit 6f90c99

Browse files
committed
docs: update README.md
1 parent e91cdb6 commit 6f90c99

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

README.md

+50-11
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55
[![StyleCI](https://github.styleci.io/repos/365018048/shield?branch=master)](https://github.styleci.io/repos/365018048?branch=master)
66
[![asyncify CI](https://github.com/ace411/asyncify/actions/workflows/ci.yml/badge.svg)](https://github.com/ace411/asyncify/actions/workflows/ci.yml)
77
[![License](http://poser.pugx.org/chemem/asyncify/license)](https://packagist.org/packages/chemem/asyncify)
8-
[![composer.lock](http://poser.pugx.org/chemem/asyncify/composerlock)](https://packagist.org/packages/chemem/asyncify)
9-
[![Dependents](http://poser.pugx.org/chemem/asyncify/dependents)](https://packagist.org/packages/chemem/asyncify)
108
[![Latest Stable Version](http://poser.pugx.org/chemem/asyncify/v)](https://packagist.org/packages/chemem/asyncify)
9+
[![PHP Version Require](http://poser.pugx.org/chemem/asyncify/require/php)](https://packagist.org/packages/chemem/asyncify)
1110

1211
</span>
1312

14-
A simple PHP library that runs your synchronous PHP functions asynchronously.
13+
A simple library with which to run blocking I/O in a non-blocking fashion.
1514

1615
## Requirements
1716

18-
- PHP 7.2 or higher
17+
- PHP 7.2 or newer
1918

2019
## Rationale
2120

22-
PHP is a largely synchronous (blocking) runtime. Asynchrony - achievable via ReactPHP and other similar suites - is a potent approach to mitigating the arduousness of I/O operations that feature prominently in day-to-day programming. Melding blocking and non-blocking routines in PHP can be a tricky proposition: when attempted haphazardly, it can yield unsightly outcomes.
23-
24-
The impetus for creating and maintaining `asyncify` is combining blocking and non-blocking PHP. Built atop ReactPHP, `asyncify` is a tool that allows one to run blocking PHP functions in an event-driven I/O environment.
21+
PHP is home to a host of functions that condition CPU idleness between successive (serial) executions—blocking functions. The expense of blocking calls—invocations of such functions—is such that they can, when deployed haphazardly in evented systems, inflict unnecessary CPU waiting behavior whilst the kernel attempts to interleave non-blocking calls. `asyncify` is a bridge between the blocking I/O in the language userspace and the evented I/O in ReactPHP. It allows those who choose to avail themselves of it the ability to run their blocking code, with minimal plumbing, in evented systems, without derailing them.
2522

2623
## Installation
2724

@@ -31,6 +28,12 @@ Though it is possible to clone the repo, Composer remains the best tool for inst
3128
$ composer require chemem/asyncify
3229
```
3330

31+
Newer versions of the library prioritize multithreading. The precondition for operationalizing multithreading is installing the [parallel](https://github.com/krakjoe/parallel) extension (`ext-parallel`) and [`react-parallel/runtime`](https://github.com/reactphp-parallel/runtime) library which can be done in a single step as in the snippet below.
32+
33+
```sh
34+
$ pie install pecl/parallel ; composer require react-parallel/runtime
35+
```
36+
3437
## Usage
3538

3639
If you want to take a Functional Programming approach, facilitated by currying, the example below should suffice.
@@ -74,7 +77,33 @@ The examples directory contains more nuanced uses of the library that I recommen
7477

7578
- `asyncify` is no panacea, but is capable of asynchronously executing a plethora of blocking calls. As presently constituted, the library is **incapable of processing inputs and outputs that cannot be serialized**. Its quintessential asynchronous function application primitive - `call()` - works almost exclusively with string encodings of native language functions and lambdas imported via an autoloading mechanism.
7679

77-
- The library cannot parse closures. All executable arbitrary code should be emplaced in a string whose sole constituent is an immediately invokable anonymous function the format of which is `(function (...$args) { /* signature */ })`.
80+
- The library, in its default configuration, cannot parse closures. All executable arbitrary code should be emplaced in a string whose sole constituent is an immediately invokable anonymous function the format of which is `(function (...$args) { /* signature */ })`.
81+
82+
## Multithreading
83+
84+
With multithreading enabled, it is possible to invoke closures and other lambdas without necessarily representing them as strings. Although string encodings are still workable, lambdas like closures should be the preferred option for representing arbitrary blocking logic. The code in the following example should work with multithreading enabled.
85+
86+
```php
87+
use function Chemem\Asyncify\call;
88+
89+
$exec = call(
90+
function (...$args) {
91+
return \file_get_contents(...$args);
92+
},
93+
['/path/to/file']
94+
);
95+
96+
$exec->then(
97+
function (string $contents) {
98+
echo $contents;
99+
},
100+
function (\Throwable $err) {
101+
echo $err->getMessage();
102+
}
103+
);
104+
```
105+
106+
> It must be noted that string representations of lambdas (anonymous functions, closures and such) that are compatible with the default child process configuration, are not usable in versions that support multithreading.
78107
79108
## API Reference
80109

@@ -83,11 +112,16 @@ The examples directory contains more nuanced uses of the library that I recommen
83112
```php
84113
namespace Chemem\Asyncify;
85114

115+
use React\{
116+
EventLoop\LoopInterface,
117+
Promise\PromiseInterface,
118+
};
119+
86120
class Async {
87121

88122
/* Methods */
89-
public static create( ?string $autoload = null [, ?React\EventLoop\LoopInterface $rootDir = null ] ) : Async;
90-
public function call( string $function [, array $args ] ) : React\Promise\PromiseInterface;
123+
public static create( ?string $autoload = null [, ?LoopInterface $rootDir = null ] ) : Async;
124+
public function call( string|callable $function [, array $args ] ) : PromiseInterface;
91125
}
92126
```
93127

@@ -100,7 +134,12 @@ class Async {
100134
```php
101135
namespace Chemem\Asyncify;
102136

103-
call ( string $func [, array $args [, ?string $autoload = null [, ?React\EventLoop\LoopInterface $args = null ] ] ] ) : React\Promise\PromiseInterface;
137+
use React\{
138+
EventLoop\LoopInterface,
139+
Promise\PromiseInterface,
140+
};
141+
142+
call ( string|callable $func [, array $args [, ?string $autoload = null [, ?LoopInterface $args = null ] ] ] ) : PromiseInterface;
104143
```
105144

106145
`call` - Curryied function that bootstraps asynchronous function calls

0 commit comments

Comments
 (0)