Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Karthik_hebbar's avatar

Implementing multithreading for a huge array in laravel.

Hi guys, I am new to laravel and php and i need a hand. I have been assigned a task in the project i am working. here a huge array containing phone numbers, where i loop through that array and perform some logic on them. The problem is, the process is very slow. so i have been tasked with splitting the for loop process and running them simultaneously. So, i figured i will use multithreading (thinking there is a package for it like in python), I did some research and found pthread. for the life of me, i am not able to understand it ,least of all implement it. so I am looking into it.

So i was hoping whether there are any other solutions.

0 likes
4 replies
Karthik_hebbar's avatar

@Sinnbeck, Thanks for the suggestion!. i tried the package, and installed the necessary php extensions, and tried below mentioned test code

use Spatie\Async\Task;
class MyTask extends Task
{
    public function configure()
    {
        // Setup eg. dependency container, load config,...
    }
    public function run()
    {
        // Do the real work here.
    }
}
// Add the task to the pool
$pool->add(new MyTask());

I got the below mentioned error: sh: 1: exec: : Permission denied {"exception":"[object] (Spatie\Async\Output\ParallelError(code: 0): sh: 1: exec: : Permission denied at /var/www/html/vendor/spatie/async/src/Output/ParallelError.php:11)

I tried the laravel wrapper for this package, But that package didn't work. For local Developement, I am using windows PC with Window subsystem linux with ubuntu. If you have happened to come across this error before, please shade some light on this. Thanks!

Sinnbeck's avatar

@Karthik_hebbar Seems that php is not able to execute the file for some reason. Sadly I have not come across that error before myself (running directly on linux)

Karthik_hebbar's avatar

@Sinnbeck, okay, i will look into it, maybe it is because of the WSL, I will try in a linux machine once. thanks!

Please or to participate in this conversation.