lukaszaleckas/laravel-concurrency is a Laravel package.
It currently has 0 GitHub stars and 9 downloads on Packagist (latest version v1.0.0).
Install it with composer require lukaszaleckas/laravel-concurrency.
Discover more Laravel packages by lukaszaleckas
or browse all Laravel packages to compare alternatives.
Last updated
composer require lukaszaleckas/laravel-concurrency
Service provider should be automatically registered, if not add
LaravelConcurrency\ConcurrencyServiceProvider::class
to your application's app.php.
php artisan vendor:publish --provider="LaravelConcurrency\ConcurrencyServiceProvider"
tasks table:php artisan migrate
Create a new task which implements LaravelConcurrency\Contracts\TaskInterface:
use LaravelConcurrency\Contracts\TaskInterface;
class SleepForTwoSeconds implements TaskInterface
{
public function run(): mixed
{
sleep(2);
return null;
}
}
Tasks return result which you want to get after running them. This can be anything you want, but it needs to be serializable.
In this example we just return null.
Tasks are run by tasks workers. To spawn a task worker run:
php artisan concurrency:run-task-worker
One worker can run only one task at a time. So we'll spawn two of those for this example.
Now run a couple of these tasks concurrently:
use LaravelConcurrency\Facades\Concurrency;
Concurrency::wait(
new SleepForTwoSeconds(),
new SleepForTwoSeconds()
)
If you notice, these tasks were both completed in ~2s, which means they've run concurrently.
You should also notice task workers logs that indicate which task ran in which process.