You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/chemem/asyncify)
11
10
12
11
</span>
13
12
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.
15
14
16
15
## Requirements
17
16
18
-
- PHP 7.2 or higher
17
+
- PHP 7.2 or newer
19
18
20
19
## Rationale
21
20
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.
25
22
26
23
## Installation
27
24
@@ -31,6 +28,14 @@ Though it is possible to clone the repo, Composer remains the best tool for inst
31
28
$ composer require chemem/asyncify
32
29
```
33
30
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 with the directives in the snippet below.
32
+
33
+
```sh
34
+
$ pie install pecl/parallel
35
+
$ echo"\nextension=parallel">>"/path/to/php.ini"
36
+
$ composer require react-parallel/runtime
37
+
```
38
+
34
39
## Usage
35
40
36
41
If you want to take a Functional Programming approach, facilitated by currying, the example below should suffice.
@@ -74,7 +79,33 @@ The examples directory contains more nuanced uses of the library that I recommen
74
79
75
80
-`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.
76
81
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 */ })`.
82
+
- 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 */ })`.
83
+
84
+
## Multithreading
85
+
86
+
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.
87
+
88
+
```php
89
+
use function Chemem\Asyncify\call;
90
+
91
+
$exec = call(
92
+
function (...$args) {
93
+
return \file_get_contents(...$args);
94
+
},
95
+
['/path/to/file']
96
+
);
97
+
98
+
$exec->then(
99
+
function (string $contents) {
100
+
echo $contents;
101
+
},
102
+
function (\Throwable $err) {
103
+
echo $err->getMessage();
104
+
}
105
+
);
106
+
```
107
+
108
+
> 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.
78
109
79
110
## API Reference
80
111
@@ -83,11 +114,16 @@ The examples directory contains more nuanced uses of the library that I recommen
0 commit comments