jubianchi / async-generator
PHP 的异步生成器运行时
dev-master
2017-03-18 17:49 UTC
Requires
- php: ^7.0.3
Requires (Dev)
- atoum/atoum: @stable
- atoum/reports-extension: @stable
- couscous/couscous: @stable
- fabpot/php-cs-fixer: @stable
This package is auto-updated.
Last update: 2024-09-04 17:18:45 UTC
README
该项目处于早期 alpha 阶段,更像是一个原型而不是真正的生产库。
要求
async-generator 库需要 PHP ^7.0.3
(即 PHP >= 7.0.3 && < 8.0.0
)
安装
使用 Composer 将此库安装到您的项目中
{ "require": { "jubianchi/async-generator": "@stable" } }
然后运行 composer up jubianchi/async-generator
,一切应该就绪。
如果您不想手动编辑您的 composer.json
文件,只需运行 composer require jubianchi/async-generator
即可。
测试
一切安装完成后,创建一个简单的 PHP 文件
<?php //index.php require_once __DIR__ . '/vendor/autoload.php'; use function jubianchi\async\runtime\{await, all}; use function jubianchi\async\time\{delay}; await( all( delay(5000, function() { echo 'World!'; }), delay(2000, function() { echo 'Hello '; }) ) );
使用 time php index.php
运行此脚本:如果一切正常,您应该在 2 秒后看到单词 Hello
,在 5 秒后看到 World!
Hello World!
real 0m5.029s
user 0m4.234s
sys 0m0.317s
请注意,整个脚本完成大约需要 5 秒,而总延迟为 7 秒:这就是这个库背后的所有魔法。它允许您使用 PHP 编写 并发 任务!