chemem/asyncify
Composer 安装命令:
composer require chemem/asyncify
包简介
A simple library with which to run blocking I/O in a non-blocking fashion.
README 文档
README
A simple library with which to run blocking I/O in a non-blocking fashion.
Requirements
- PHP 7.2 or newer
Rationale
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.
Installation
Though it is possible to clone the repo, Composer remains the best tool for installing asyncify. To install the package via Composer, type the following in a console of your choosing.
$ composer require chemem/asyncify
Newer versions of the library prioritize multithreading. The precondition for operationalizing multithreading is installing the parallel extension (ext-parallel) and react-parallel/runtime library which can be done with the directives in the snippet below.
$ pie install pecl/parallel $ echo "\nextension=parallel" >> "/path/to/php.ini" $ composer require react-parallel/runtime
Usage
If you want to take a Functional Programming approach, facilitated by currying, the example below should suffice.
use function Chemem\Asyncify\call; $call = call('file_get_contents', ['foo.txt']) ->then( function (?string $contents) { echo $contents; }, function (\Throwable $err) { echo $err->getMessage(); } );
Or
If you prefer a more conventional OOP approach, the snippet below should prove apt.
use Chemem\Asyncify\Async; $exec = Async::create() ->call('file_get_contents', ['foo.txt']) ->then( function (?string $contents) { echo $contents; }, function (\Throwable $err) { echo $err->getMessage(); } );
The examples directory contains more nuanced uses of the library that I recommend you check out.
Limitations
-
asyncifyis 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. -
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 */ }).
Multithreading
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.
use function Chemem\Asyncify\call; $exec = call( function (...$args) { return \file_get_contents(...$args); }, ['/path/to/file'] ); $exec->then( function (string $contents) { echo $contents; }, function (\Throwable $err) { echo $err->getMessage(); } );
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.
API Reference
Object
namespace Chemem\Asyncify; use React\{ EventLoop\LoopInterface, Promise\PromiseInterface, }; class Async { /* Methods */ public static create( ?string $autoload = null [, ?LoopInterface $rootDir = null ] ) : Async; public function call( string|callable $function [, array $args ] ) : PromiseInterface; }
Async::__construct - Creates a new Async object instance
Async::call - Asynchronously calls a synchronous (blocking) PHP function
Function
namespace Chemem\Asyncify; use React\{ EventLoop\LoopInterface, Promise\PromiseInterface, }; call ( string|callable $func [, array $args [, ?string $autoload = null [, ?LoopInterface $args = null ] ] ] ) : PromiseInterface;
call - Curryied function that bootstraps asynchronous function calls
Important Considerations
asyncify, by default, utilizes the autoload file (autoload.php) in thevendordirectory of the composer project in which it resides.- The library converts all errors in the functions slated for non-blocking execution to exceptions.
Dealing with problems
Endeavor to create an issue on GitHub when the need arises or send an email to lochbm@gmail.com.
Contributing
Consider buying me a coffee if you appreciate the offerings of the project and/or would like to provide more impetus for me to continue working on it.
chemem/asyncify 适用场景与选型建议
chemem/asyncify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 247 次下载、GitHub Stars 达 30, 最近一次更新时间为 2021 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chemem/asyncify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chemem/asyncify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 247
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2021-08-05