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,12 @@ 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 in a single step as in the snippet below.
32
+
33
+
```sh
34
+
$ pie install pecl/parallel ; composer require react-parallel/runtime
35
+
```
36
+
34
37
## Usage
35
38
36
39
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
74
77
75
78
-`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
79
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.
78
107
79
108
## API Reference
80
109
@@ -83,11 +112,16 @@ The examples directory contains more nuanced uses of the library that I recommen
0 commit comments