PHPackages                             proilyxa/lara-thread - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. proilyxa/lara-thread

ActiveLibrary

proilyxa/lara-thread
====================

This is my package lara-thread

0.0.2(1y ago)17MITPHPPHP ^8.1

Since Dec 31Pushed 1y ago1 watchersCompare

[ Source](https://github.com/proilyxa/lara-thread)[ Packagist](https://packagist.org/packages/proilyxa/lara-thread)[ Docs](https://github.com/proilyxa/lara-thread)[ RSS](/packages/proilyxa-lara-thread/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (8)Versions (4)Used By (0)

Lara-thread
===========

[](#lara-thread)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3bd326154dc7ace15a67e8e9bc1dee54c0c953ece6e66fa50364c2fe7a69c262/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70726f696c7978612f6c6172612d7468726561642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/proilyxa/lara-thread)[![GitHub Tests Action Status](https://camo.githubusercontent.com/2febad26896647b2ef160d94c04fd00a30bf8de953c526c076ed551a886ba283/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f70726f696c7978612f6c6172612d7468726561642f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/proilyxa/lara-thread/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/6eaddbe09dc422edc0cbdfe57f8b139ee4e4fee815551bb183b20222883168cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f70726f6c7978612f6c6172612d7468726561642f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/ilya/lara-thread/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/1885501408b16c07e07f65e45126c8ead4900f03e83649f54a9b316d29fc35ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70726f696c7978612f6c6172612d7468726561642e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/proilyxa/lara-thread)

Description
-----------

[](#description)

Since Swoole 6 it supports multithreading, so it's a great way to speed up Laravel applications!

You can use this functionality in your Laravel Octane application if you are using the Swoole driver or in console commands.

Require: Laravel 10-11, php8.1+ ZTS (Thread safe), swoole 6.0+ extension which was compiled with the --enable-swoole-thread parameter

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require proilyxa/lara-thread
```

```
php artisan vendor:publish --tag=proilyxa-lara-thread
```

Usage
-----

[](#usage)

```
public static function run(string $class, mixed ...$params): Thread
```

LaraThread::run takes as its first parameter a class that implements the run() method. The run method can have any input parameters.

Main
----

[](#main)

```
use Swoole\Thread\Queue;

$start = microtime(true);

$input = new Queue();
$output = new Queue();

$d = 20;
for ($i = 0; $i < $d; $i++) {
    $input->push('https://dog.ceo/api/breeds/image/random');
}

// create workers
$t = 5;
$threads = [];
for ($threadID = 0; $threadID < $t; $threadID++) {
    $threads[] = LaraThread::run(Run::class, $threadID + 1, $input, $output);
}

$result = [];
for ($i = 0; $i < $d; $i++) {
    $result[] = $output->pop(-1);
}

dump(LaraThread::recursiveUnserialize($result));

// waiting for threads to finish
for ($i = 0; $i < count($threads); $i++) {
    $threads[$i]->join();
}

echo 'timeline: ' . round(microtime(true) - $start, 4) . ' s.';
```

Worker
------

[](#worker)

```
