Laravel Laravel
  • Prologue

    • Release Notes
    • Upgrade Guide
    • Contribution Guide
  • Getting Started

    • Installation
    • Configuration
    • Directory Structure
    • Frontend
    • Starter Kits
    • Deployment
  • Architecture Concepts

    • Request Lifecycle
    • Service Container
    • Service Providers
    • Facades
  • The Basics

    • Routing
    • Middleware
    • CSRF Protection
    • Controllers
    • Requests
    • Responses
    • Views
    • Blade Templates
    • Asset Bundling
    • URL Generation
    • Session
    • Validation
    • Error Handling
    • Logging
  • Digging Deeper

    • Artisan Console
    • Broadcasting
    • Cache
    • Collections
    • Concurrency
    • Context
    • Contracts
    • Events
    • File Storage
    • Helpers
    • HTTP Client
    • Localization
    • Mail
    • Notifications
    • Package Development
    • Processes
    • Queues
    • Rate Limiting
    • Strings
    • Task Scheduling
  • Security

    • Authentication
    • Authorization
    • Email Verification
    • Encryption
    • Hashing
    • Password Reset
  • Database

    • Getting Started
    • Query Builder
    • Pagination
    • Migrations
    • Seeding
    • Redis
    • MongoDB
  • Eloquent ORM

    • Getting Started
    • Relationships
    • Collections
    • Mutators / Casts
    • API Resources
    • Serialization
    • Factories
  • Testing

    • Getting Started
    • HTTP Tests
    • Console Tests
    • Browser Tests
    • Database
    • Mocking
  • Packages

    • Breeze
    • Cashier (Stripe)
    • Cashier (Paddle)
    • Dusk
    • Envoy
    • Fortify
    • Folio
    • Homestead
    • Horizon
    • Jetstream
    • Mix
    • Octane
    • Passport
    • Pennant
    • Pint
    • Precognition
    • Prompts
    • Pulse
    • Reverb
    • Sail
    • Sanctum
    • Scout
    • Socialite
    • Telescope
    • Valet
  • API Documentation

Concurrency

  • Introduction
  • Running Concurrent Tasks
  • Deferring Concurrent Tasks

Introduction

[!WARNING] Laravel's Concurrency 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's Concurrency 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 the ConcurrencyServiceProvider to the providers array in your application's config/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.

The Concurrency facade supports three drivers: process (the default), fork, and sync.

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

composer require spatie/fork

The sync 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 the Concurrency facade's run method. The run method accepts an array of closures which should be executed simultaneously in child PHP processes:

use Illuminate\Support\Facades\Concurrency;
use Illuminate\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 the driver method:

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

Or, to change the default concurrency driver, you should publish the concurrency configuration file via the config:publish Artisan command and update the default 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 the defer method. When the defer 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:

use App\Services\Metrics;
use Illuminate\Support\Facades\Concurrency;

Concurrency::defer([
    fn () => Metrics::report('users'),
    fn () => Metrics::report('orders'),
]);
last update:2025-03-28 16:07

成为Laravel合作伙伴

Laravel Partners是提供一流Laravel开发和咨询服务的精英商店。我们每个合作伙伴都可以帮助您制定一个精美,结构完善的项目.

我们的伙伴
Laravel
亮点
  • Our Team
  • 发布说明
  • 入门
  • 路由
  • Blade 模板
  • 身份验证
  • 用户授权
  • Artisan 控制台
  • 数据库
  • Eloquent ORM
  • 测试
资源
  • Laravel Bootcamp
  • Laracasts
  • Laravel News
  • Laracon
  • Laracon EU
  • Laracon India
  • Jobs
  • Forums
  • Trademark
  • 版本发布时间
  • 包开发
  • 命令行应用
  • TALL stack全栈开发
  • Blade UI Kit
  • 前端资源构建
伙伴
  • WebReinvent
  • Vehikl
  • Tighten
  • 64 Robots
  • Active Logic
  • Byte 5
  • Curotec
  • Cyber-Duck
  • DevSquad
  • Jump24
  • Kirschbaum
生态系统
  • Cashier
  • Dusk
  • Echo
  • Envoyer
  • Forge
  • Horizon
  • Nova
  • Octane
  • Sail
  • Sanctum
  • Scout
  • Spark
  • Telescope
  • Valet
  • Vapor

Laravel是一个具有表达力,优雅语法的Web应用程序框架。我们认为,发展必须是一种令人愉悦的创造力,才能真正实现。Laravel试图通过减轻大多数Web项目中使用的常见任务来减轻开发的痛苦.

Laravel是Taylor Otwell的商标.
Copyright © 2011-2025 Laravel中文网 LLC.

  • Twitter
  • GitHub
  • Discord
Laravel 全栈开发网 推荐使用阿里云 按Ctrl+D试试