Skip to content

Commit ce657e9

Browse files
Update concurrency.md (laravel#9893)
* 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 * Update concurrency.md --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4fb89ea commit ce657e9

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

concurrency.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ Sometimes you may need to execute several slow tasks which do not depend on one
1717

1818
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.
1919

20-
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.
20+
The `Concurrency` facade supports three drivers: `process` (the default), `fork`, and `sync`.
21+
22+
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:
23+
24+
```bash
25+
composer require spatie/fork
26+
```
27+
28+
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.
2129

2230
<a name="running-concurrent-tasks"></a>
2331
## Running Concurrent Tasks
@@ -34,6 +42,18 @@ use Illuminate\Support\Facades\DB;
3442
]);
3543
```
3644

45+
To use a specific driver, you may use the `driver` method:
46+
47+
```php
48+
$results = Concurrency::driver('fork')->run(...);
49+
```
50+
51+
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:
52+
53+
```bash
54+
php artisan config:publish concurrency
55+
```
56+
3757
<a name="deferring-concurrent-tasks"></a>
3858
## Deferring Concurrent Tasks
3959

0 commit comments

Comments
 (0)