publicplan / parallel-bridge
Symfony Parallel Bridge
v1.1.2
2021-01-18 15:34 UTC
Requires
- php: >=7.3
- amphp/parallel-functions: ^1.0
- symfony/config: ^4.0 || ^5.0
- symfony/dependency-injection: ^4.0 || ^5.0
- symfony/http-kernel: ^4.0 || ^5.0
Requires (Dev)
- roave/security-advisories: dev-master
- symfony/phpunit-bridge: ^5.2
README
提供 AMPHP Parallel 的实用工具,特别是并行函数。此 Bundle 使用 php 7.3、7.4 和 8.0 以及 Symfony 4.4 和 5.2 进行开发和测试。其他版本可能也能工作,但不提供支持或测试。
安装设置
- 使用 composer 安装 Parallel-Bridge。
composer require publicplan/parallel-bridge
- 在您的应用程序配置文件夹中创建
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;
- 在您的
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%'
- 在您的
.env.local
中设置最大工作进程数。
AMPHP_MAX_WORKERS=3
info: 对于没有工作进程的同步执行,请填写 "0"。这对于调试和 Mac 上的一些罕见问题很有用。
使用方法
- 在您的类中使用 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; } }
- 使服务公开!当使用服务时,如上面的示例所示,您需要使您的服务公开。
# 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; } }