Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

concurrency.md

Latest commit

 

History

History
93 lines (67 loc) · 3.71 KB

concurrency.md

File metadata and controls

93 lines (67 loc) · 3.71 KB

Introduction

Warning

Laravel'sConcurrency facade is currently in beta while we gather community feedback.

Sometimes you may need to execute several slow tasks which do not depend on one another. In many cases, significant performance improvements can be realized by executing the tasks concurrently. Laravel'sConcurrency facade provides a simple, convenient API for executing closures concurrently.

Concurrency Compatibility

If you upgraded to Laravel 11.x from a Laravel 10.x application, you may need to add theConcurrencyServiceProvider to theproviders array in your application'sconfig/app.php configuration file:

'providers' => ServiceProvider::defaultProviders()->merge([/*     * Package Service Providers...     */Illuminate\Concurrency\ConcurrencyServiceProvider::class,// [tl! add]/*     * Application Service Providers...     */App\Providers\AppServiceProvider::class,App\Providers\AuthServiceProvider::class,// App\Providers\BroadcastServiceProvider::class,App\Providers\EventServiceProvider::class,App\Providers\RouteServiceProvider::class,])->toArray(),

How it Works

Laravel achieves concurrency by serializing the given closures and dispatching them to a hidden Artisan CLI command, which unserializes the closures and invokes it within its own PHP process. After the closure has been invoked, the resulting value is serialized back to the parent process.

TheConcurrency facade supports three drivers:process (the default),fork, andsync.

Thefork driver offers improved performance compared to the defaultprocess driver, but it may only be used within PHP's CLI context, as PHP does not support forking during web requests. Before using thefork driver, you need to install thespatie/fork package:

composer require spatie/fork

Thesync driver is primarily useful during testing when you want to disable all concurrency and simply execute the given closures in sequence within the parent process.

Running Concurrent Tasks

To run concurrent tasks, you may invoke theConcurrency facade'srun method. Therun method accepts an array of closures which should be executed simultaneously in child PHP processes:

useIlluminate\Support\Facades\Concurrency;useIlluminate\Support\Facades\DB;[$userCount,$orderCount] = Concurrency::run([fn () =>DB::table('users')->count(),fn () =>DB::table('orders')->count(),]);

To use a specific driver, you may use thedriver method:

$results = Concurrency::driver('fork')->run(...);

Or, to change the default concurrency driver, you should publish theconcurrency configuration file via theconfig:publish Artisan command and update thedefault option within the file:

php artisan config:publish concurrency

Deferring Concurrent Tasks

If you would like to execute an array of closures concurrently, but are not interested in the results returned by those closures, you should consider using thedefer method. When thedefer method is invoked, the given closures are not executed immediately. Instead, Laravel will execute the closures concurrently after the HTTP response has been sent to the user:

useApp\Services\Metrics;useIlluminate\Support\Facades\Concurrency;Concurrency::defer([fn () => Metrics::report('users'),fn () => Metrics::report('orders'),]);

[8]ページ先頭

©2009-2025 Movatter.jp