Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qbhy/swoole-parallel
可以并行执行协程的库
https://github.com/qbhy/swoole-parallel
Last synced: about 1 month ago
JSON representation
可以并行执行协程的库
- Host: GitHub
- URL: https://github.com/qbhy/swoole-parallel
- Owner: qbhy
- License: mit
- Created: 2020-06-15T05:24:35.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-15T06:39:57.000Z (over 4 years ago)
- Last Synced: 2024-05-01T03:22:53.299Z (8 months ago)
- Language: PHP
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## swoole-parallel
可以并行执行协程的库,摘自 hyperf/utils 由于hyperf/utils 的 parallel 只能在 hyperf 框架下面使用,所以有了这个项目。## 安装 - install
```bash
$ composer require qbhy/swoole-parallel
```## 使用 - usage
```phprequire __DIR__ . 'vendor/autoload.php';
\Swoole\Coroutine::create(function () {
$time = time();
$parallel = new \Qbhy\SwooleParallel\Parallel(20);// $parallel->setCaller(function ($callback) {
// return app()->call($callback); // laravel 内可以这样实现协程内依赖注入
// });for ($i = 0; $i < 5; ++$i) {
$parallel->add(function () use ($i) {
\Swoole\Coroutine::sleep(1);
return $i;
});
}$results = $parallel->wait();
$diff = time() - $time;
var_dump($results);
print_r("运行了:{$diff}秒");
});
```
运行结果
```
array(5) {
[0]=>
int(0)
[4]=>
int(4)
[3]=>
int(3)
[2]=>
int(2)
[1]=>
int(1)
}
运行了:1秒
```
> 请务必在协程环境下使用