vladidas/parallelphp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vladidas/parallelphp

Composer 安装命令:

composer require vladidas/parallelphp

包简介

Running PHP functions in parallel thread without blocking.

README 文档

README

Minimal requirements:

PHP 7.0

A few words about the package:

This package allows you to call functions and execute them in parallel without waiting for the previous one to complete. This allows you to speed up the execution of the script as a whole.

For example:

<?php
   // You have three functions:
   $functions = [
       function () {
           sleep(1);
           return 11;
       },
       function () {
           sleep(2);
           return 22;
       },
       function () {
           sleep(3);
           return 33;
       }
   ];
   
   // If you do them synchronously:
   $functions[0](); // 1 sec.
   $functions[1](); // 2 sec.
   $functions[2](); // 3 sec.
   // Spend = 6 sec.
<?php
    // If you do them using parallel calling:
    $promise = new Promise();
    $promise
        ->addItem(function () {
            sleep(1);
            return 11;
        })
        ->addItem(function () {
            sleep(2);
            return 22;
        })
        ->addItem(function () {
            sleep(3);
            return 33;
        })
        ->then(function ($first, $second, $third) {
            var_dump('then #1:', $first, $second, $third);
        })
        ->resolve(function ($first, $second, $third) {
            var_dump('resolve: ', $first, $second, $third);
        })
        ->finally(function ($first, $second, $third) {
            var_dump('finally: ', $first, $second, $third);
        });
    // Spend 3 sec.

Great, isn't it?

This package is just a decorator between AmPHP and you using Promise methods to increase ease of use.

Examples:

<?php

$promise = new Promise();

/**
 * Add items.
 */
$promise
    ->addItem(function () {
        sleep(1);
        return 11;
    })
    ->addItem(function () {
        sleep(2);
        return 22;
    })
    ->addItem(function () {
        sleep(3);
        return 33;
    });

/**
 * Remove items by key.
 */
if ($conditions) {
    $promise
        ->removeItem(2)
        ->removeItem(3);
}

/**
 * Starting to execute the event list.
 */
$promise
    ->then(function ($first, $second, $third) {
        var_dump('then #1:', $first, $second, $third);
    })
    ->then(function ($first, $second, $third) {
        var_dump('then #2:', $first, $second, $third);
    });

/**
 * If the list items were resolved.
 */
$promise
    ->resolve(function ($first, $second, $third) {
        var_dump('resolve: ', $first, $second, $third);
    });

/**
 * If the list items were rejected.
 */
$promise
    ->reject(function ($exception) {
        var_dump('reject: ', $exception);
    });

/**
 * The final method to complete.
 */
$promise
    ->finally(function ($first, $second, $third) {
        var_dump('finally: ', $first, $second, $third);
    });

In the case of Laravel it can be considered as:

$promise
    ->addItem(function () {
        $users = \DB::table('users')->get();
        // Some slow process
        return $users;
    })
    ->addItem(function () {
        $products = \DB::table('products')->get();
        // Some slow process
        return $products;
    });

You can also add items using __constructor in Promise class:

<?php

$promise = new Promise([
    function () {
        sleep(1);
        return 11;
    },
    function () {
        sleep(2);
        return 22;
    },
    function () {
        sleep(3);
        return 33;
    }
]);

...

$promise
    ->addItem(function () {
        sleep(1);
        return 11;
    })
    ->removeItem(2)
    ->then(function ($first, $second, $third) {
        var_dump('then #1:', $first, $second, $third);
    });

...

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固