From 126714771f3c11208719dbf7ce23ef9ae6b5aa10 Mon Sep 17 00:00:00 2001 From: Giancarlo Di Massa Date: Fri, 13 Sep 2024 10:57:25 +0200 Subject: [PATCH 1/2] Update concurrency.md - Add requirements of spatie/fork - Show example on how to switch drivers - Explain that the defer method will never run the tasks in the PHP CLI's context --- concurrency.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/concurrency.md b/concurrency.md index 114950d9f38..61a568d304a 100644 --- a/concurrency.md +++ b/concurrency.md @@ -17,7 +17,16 @@ Sometimes you may need to execute several slow tasks which do not depend on one 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. The `sync` driver is primarily useful during testing when you want to disable all concurrency and simple execute the given closure in sequence within 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: + +```bash +composer require spatie/fork +``` + +The `sync` driver is primarily useful during testing when you want to disable all concurrency and simple execute the given closure in sequence within the parent process. ## Running Concurrent Tasks @@ -48,3 +57,18 @@ Concurrency::defer([ fn () => Metrics::report('orders'), ]); ``` + +The `defer` method cannot be used within PHP's CLI context as no HTTP response is generated and the task will never run. + + +## Switching the Concurrency Driver + +Laravel's `Concurrency` facade supports multiple drivers. + +To switch the driver, use this syntax: + +```php +use Illuminate\Support\Facades\Concurrency; + +Concurrency::setDefaultInstance('fork'); +``` From 5d6cd00ae30e7a9fd8b7357e0eafb9f89a65cd5d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 13 Sep 2024 11:05:52 -0500 Subject: [PATCH 2/2] Update concurrency.md --- concurrency.md | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/concurrency.md b/concurrency.md index 61a568d304a..d849f6049a1 100644 --- a/concurrency.md +++ b/concurrency.md @@ -19,14 +19,13 @@ Laravel achieves concurrency by serializing the given closures and dispatching t 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: ```bash composer require spatie/fork ``` -The `sync` driver is primarily useful during testing when you want to disable all concurrency and simple execute the given closure in sequence within the parent process. +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 @@ -43,6 +42,18 @@ use Illuminate\Support\Facades\DB; ]); ``` +To use a specific driver, you may use the `driver` method: + +```php +$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: + +```bash +php artisan config:publish concurrency +``` + ## Deferring Concurrent Tasks @@ -57,18 +68,3 @@ Concurrency::defer([ fn () => Metrics::report('orders'), ]); ``` - -The `defer` method cannot be used within PHP's CLI context as no HTTP response is generated and the task will never run. - - -## Switching the Concurrency Driver - -Laravel's `Concurrency` facade supports multiple drivers. - -To switch the driver, use this syntax: - -```php -use Illuminate\Support\Facades\Concurrency; - -Concurrency::setDefaultInstance('fork'); -```