sflightning / bundle
Symfony扩展,通过闪电库支持Swoole
dev-main
2022-10-24 18:42 UTC
Requires
- php: >=7.3
- sflightning/contracts: dev-main
- sflightning/lib: dev-main
- sflightning/runtime: dev-main
- symfony/config: 5.4.x-dev
- symfony/dependency-injection: 5.4.x-dev
- symfony/http-kernel: 5.4.x-dev
Conflicts
- ext-swoole: <4.6.0
This package is auto-updated.
Last update: 2024-09-24 22:43:01 UTC
README
一个用于Symfony应用的bundle,该应用使用Swoole。
此bundle提供了使用Swoole与Symfony之间的桥梁,并试图简化Swoole并发使用。
此bundle附带其自己的Runtime、Contracts和Implementation。
本项目受到优秀的Laravel Octane的启发,并试图为Symfony应用提供一个良好的替代方案!
仍在开发中
安装
composer require sflightning/bundle
使用
此bundle附带其自己的Promise定义:Sflightning\Contracts\Concurrency\Promise\LightningPromiseInterface
和实现:Sflightning\Lib\Concurrency\Promise
。
创建Promise
您可以通过构造函数或使用静态构建器buildFromCallable
来创建Promise。在两种情况下,您都需要提供一个callable
,它表示要并发执行的任务。
use Sflightning\Lib\Concurrency\Promise\Promise;
$promise = new Promise(function(callable $resolve, callable $reject) {
// Your async task here;
});
$promise->then(function ($result) {
// If your task succeed;
});
$promise->catch(function ($result) {
// If your task gone wrong;
});
$promise->finally(function () {
// And in any case;
});
或者您也可以使用静态构建器
$promise = Promise::buildFromCallable(function(callable $resolve, callable $reject) {
// Your async task here;
})->then(function ($result) {
// If your task succeed;
})->catch(function ($result) {
// If your task gone wrong;
})->finally(function () {
// And in any case;
});
异步执行您的Promise
现在您已经创建了Promise,是时候执行它们了!(并发执行)。为此,很简单:您只需实现Sflightning\Bundle\Concurrency\LightningConcurrencyAware
并使用特质Sflightning\Bundle\Concurrency\LightningConcurrency
。现在您的对象有三种新的方法可用
- executePromise(LightningPromiseInterface $promise): void : 异步执行给定的Promise。此方法是非阻塞的。
- executePromises(...$promises): void : 异步执行给定的Promise。此方法是非阻塞的。
- waitForPromise(LightningPromiseInterface $promise, int $timeout = Concurrency::PROMISE_WAIT_INFINITELY): void : 等待给定的Promise完成。这是此特质中唯一的阻塞方法!
class MyAsyncService implements LightningConcurrencyAware
{
use LightningConcurrency;
public function doSmth(...$args): void
{
$promise = Promise::buildFromCallable(function(callable $resolve, callable $reject) {
// Your async task here;
})->then(function ($result) {
// If your task succeed;
})->catch(function ($result) {
// If your task gone wrong;
})->finally(function () {
// And in any case;
});
$this->executePromise($promise);
// More code
$this->waitForPromise($promise);
}
}
您也可以在一个服务中启动Promise,并在调用控制器的方法中等待它
// MyAsyncService.php
class MyAsyncService implements LightningConcurrencyAware
{
use LightningConcurrency;
public function doSmth(LightningPromiseInterface $promise): void
{
// Your code
$this->executePromise($promise);
// More code
// Notice that we don't do any waitForPromise here, it's the calling object duty for this example !
}
}
// TestController.php
class TestController implements LightningConcurrencyAware
{
use LightningConcurrency;
public function method1(Request $request, MyAsyncService $myAsyncServiceInstance): void
{
$promise = Promise::buildFromCallable(function(callable $resolve, callable $reject) {
// Your async task here;
});
$myAsyncServiceInstance->doSmth($promise);
$this->waitForPromise($promise);
return new Response('OK', 200);
}
}