publicplan/parallel-bridge

Symfony Parallel Bridge

安装次数: 14,938

依赖: 0

建议者: 0

安全: 0

星标: 3

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

v1.1.2 2021-01-18 15:34 UTC

This package is auto-updated.

Last update: 2024-09-18 23:36:12 UTC


README

Version MIT License Symfony Pipeline

提供 AMPHP Parallel 的实用工具,特别是并行函数。此 Bundle 使用 php 7.3、7.4 和 8.0 以及 Symfony 4.4 和 5.2 进行开发和测试。其他版本可能也能工作,但不提供支持或测试。

安装设置

  1. 使用 composer 安装 Parallel-Bridge。
composer require publicplan/parallel-bridge
  1. 在您的应用程序配置文件夹中创建 worker-bootstrap.php
// app/config/worker-bootstrap.php
<?php

declare(strict_types=1);

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

set_time_limit(0);

if (class_exists(Dotenv::class)) {
    if (method_exists(Dotenv::class, 'bootenv')) {
        (new Dotenv())->bootEnv(dirname(__DIR__) . '/.env');
    } else {
        require dirname(__DIR__) . '/config/bootstrap.php';
    }
}

$kernel = new Kernel($_SERVER['APP_ENV'], (bool)$_SERVER['APP_DEBUG']);
$kernel->boot();
$GLOBALS['kernel'] = $kernel;
  1. 在您的 config/packages 目录中创建 parallel_bridge.yaml
# config/packages/parallel_bridge.yaml
publicplan_parallel_bridge:
  amphp_max_worker: '%env(int:AMPHP_MAX_WORKERS)%'
  project_dir: '%kernel.project_dir%'
  1. 在您的 .env.local 中设置最大工作进程数。
AMPHP_MAX_WORKERS=3

info: 对于没有工作进程的同步执行,请填写 "0"。这对于调试和 Mac 上的一些罕见问题很有用。

使用方法

  1. 在您的类中使用 PromiseWait 来重映射异步!您可以使用任何喜欢的可调用函数,但应考虑闭包必须是可序列化的。当您需要项目上下文时,我们建议使用以下方式:(也请参阅 https://amphp.org/parallel-functions/ 以获取更多信息)
// src/Service/YourClass.php
<?php

declare(strict_types=1);

namespace App\Service;

use Amp\MultiReasonException;
use Publicplan\ParallelBridge\PromiseWaitInterface;

class YourClass
{
    /** @var PromiseWaitInterface */
    private $promiseWait;

    public function __construct(PromiseWaitInterface $promiseWait)
    {
        $this->promiseWait = $promiseWait;
    }

    public function yourFunction(): void
    {
        $unprocessDataArray = range(1, 100);

        try{
            //If we want to use a container class it must be public and in the following format:
            $finalDataArray = $this->promiseWait->parallelMap($unprocessDataArray, [$this,'processSingleElement']);
        } 
        //to get possible errors you have to catch MultiReasonException 
        catch (MultiReasonException $exception ){
            dd($exception);
        }
        print_r($finalDataArray);
    }

    //This Function will be called async from our processes after grabbing this service from service container
    public function processSingleElement(int $number): string
    {
        for ($i = 0; $i < 200000; $i++) {
         $hash =  hash('SHA512', (string)$number);
        }
        return $hash;
    }
}
  1. 使服务公开!当使用服务时,如上面的示例所示,您需要使您的服务公开。
# config/services.yaml
services:
    [...]
    App\Service\YourClass:
        public: true

可选:附加参数

根据需要向 PromiseWait::parallelMap() 函数添加附加参数。所有参数都会传递到您的调用函数中。

<?php

declare(strict_types=1);

namespace App\Service;

use Publicplan\ParallelBridge\PromiseWaitInterface;

class YourClass
{
    /** @var PromiseWaitInterface */
    private $promiseWait;

    public function __construct(PromiseWaitInterface $promiseWait)
    {
        $this->promiseWait = $promiseWait;
    }
    
    public function yourFunction(): void
    {
        $unprocessDataArray = range(1, 100);
        $additionalArg = 42;

        $result = $this->promiseWait->parallelMap(
           $unprocessDataArray, 
           [$this,'processSingleElement'],
           $additionalArg,
        );
        
        //Returns numbers from 43 to 143
        print_r($result);
    }

    public function processSingleElement(int $number, int $additionalArg): int
    {
        return $number + $additionalArg;
    }
}