tohidplus/paravel

A package to run anonymous functions in parallel

2.0.2 2020-10-19 12:18 UTC

This package is auto-updated.

Last update: 2024-10-19 21:32:10 UTC


README

GitHub issues GitHub stars Total Downloads Code Quality GitHub license

Parallel and Asynchronous functions in Laravel

A Laravel package to run anonymous functions in parallel or asynchronously without need to install any extension.

Installation

composer require tohidplus/paravel

Laravel

Publish the config file

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

Lumen

Copy the config file paravel.php from vendor/tohidplus/paravel to config directory and configure it in bootstrap/app.php

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

Configuration

return [
    'artisan_path' => env('PARAVEL_ARTISAN_PATH', base_path('artisan')),
];
  • Make sure the artisan path is correct.

Basic example

<?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

Run functions in background

<?php

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

Helper methods

<?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();

Notice: Paravel comes with 100 milliseconds overhead by default. So before starting to use this package make sure the total execution time of processes is over 100ms.

Contribution

Please feel free to open issues or have contribution.