tohidplus/paravel

并行运行匿名函数的包

2.0.2 2020-10-19 12:18 UTC

This package is auto-updated.

Last update: 2024-09-19 21:21:26 UTC


README

GitHub issues GitHub stars Total Downloads Code Quality GitHub license

Laravel中的并行和异步函数

一个Laravel包,用于并行或异步运行匿名函数,无需安装任何扩展。

安装

composer require tohidplus/paravel

Laravel

发布配置文件

php artisan vendor:publish --provider="Tohidplus\Paravel\ParavelServiceProvider"

Lumen

将配置文件 paravel.phpvendor/tohidplus/paravel 复制到 config 目录,并在 bootstrap/app.php 中进行配置

$app->configure('paravel');
//
$app->register(Tohidplus\Paravel\ParavelServiceProvider::class);

配置

return [
    'artisan_path' => env('PARAVEL_ARTISAN_PATH', base_path('artisan')),
];
  • 确保 artisan 路径正确。

基本示例

<?php
use Tohidplus\Paravel\Facades\Paravel;

$time = microtime(true);        

$results = Paravel::add('label_1',function (){
    sleep(5);
    return 'Hello there';
})->add('label_2',function (){
     sleep(5);
     return 'Hello again';
})->wait();

//Check the total execution time
dump(microtime(true)-$time); // 5.* Secs

在后台运行函数

<?php

use Tohidplus\Paravel\Facades\Paravel;
   
Paravel::add('label_1',function (){
    return 'Hello there';
})->add('label_2',function (){
     return 'Hello again';
})->run();

辅助方法

<?php

use Tohidplus\Paravel\Facades\Paravel;

$results = Paravel::add('label_1',function (){
    return 'Hello there';
})->add('label_2',function (){
     return 'Hello again';
})->wait();

// Get the item by label
$results->get('label_1');
// Get result of item
$results->resultOf('label_1');
// Get error of item
$results->errorOf('label_1');
// Get status of item
$results->statusOf('label_1');

// Check if all processes were succeeded.
$results->succeeded();
// Check if any of the processes was failed.
$results->failed();

注意: Paravel 默认有 100毫秒 的开销。所以在开始使用此包之前,请确保进程的总执行时间超过100ms。

贡献

请随时提交问题或进行贡献。