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

adityar15's avatar

Multi threading in Laravel

So I have a function in Controller which will be doing heavy computations. I am trying to achieve concurrent programming to get this task done by multiple threads. I tried researching on symphony process but its mostly for artisan commands. How can I achieve using multithreading to split and do the task in a controller function between multiple threads?

0 likes
10 replies
adityar15's avatar

I have used queues before. But I don’t know how to receive the output of the queue as I will require to fetch the output to display it to the user on front end. If I am right, queues run in the background and are good for tasks where you do not require a return value eg sending emails, updating database etc.

2 likes
rodrigo.pedra's avatar

You can have an auxiliary table/model where you track your job status and save the results. So in the frontend you would show the processing status while it processes and keep refreshing (or polling with ajax) the status until it is done.

Imagine you need to perform time expensive operations on an Order model. Your job would be something like this:

class GenerateExpensiveReport implements ShouldQueue {
  use SerializesModels;

  protected $order;

  // pass any other needed variables to the job
  public function __construct(Order $order) {
    $this->order = $order;
  }

  public function handle() {
    $process = new Process();
    $process->status = 'processing';
    $process->output = null;
    $process->started_at = now();
    $process->ended_at = null;
    $this->order->process()->save($process);

    try {
      // perform some time expensive calculation

      $process->status = 'success';
      $process->output = null;
      $process->ended_at = now();
      $process->save();
    } catch(\Throwable $exception) { // use \Exception on older PHP version
      $process->status = 'error';
      $process->output = $exception->getMessage();
      $process->ended_at = now();
      $process->save();
    }
  }
}

The Process model needs a table with a status, output, started_at and ended_at and morph columns.

https://laravel.com/docs/7.x/eloquent-relationships#one-to-one-polymorphic-relations

And in your Order model you define the relation to process as this:

public function process()
{
  return $this->morphOne(Process::class, 'processable')
    ->withDefault(function () {
      return Process(['status' => 'pending']);
    });
}

Of course you can add those snippets to update a process status on the status model itself so you can reuse with other jobs

adityar15's avatar

I see. Thanks @rodrigo.pedra for a detailed explanation. Very much appreciated. So, its to have a table where results are stored after the job is processed and once the results are ready to collect from the table, fetch them and display to the user or use it for further processing if required. I will give it a try. I was more looking like Python's threading module equivalent for PHP. And seems like pthread of PHP is not made for web servers. So the options either are queuing the jobs or synchronous code with code optimisation.

1 like
rodrigo.pedra's avatar

I guess you can enable threads if you are using php-fpm (usually with nginx) but you will need to whitelist and increase timeout in your php.ini

I wouldn't go that way, as it is not usual.

But considering your description I would agree that the options are using queued jobs or optimized synchronous code.

Personally I like the flow of queuing jobs and have a record to inform the user of the progress and completion/failure. I think UI is more responsive as the request just dispatches the job and gets back to the user.

But of course your requirements might ask for a different approach.

Good luck, and please share if you come to a different strategy.

1 like
adityar15's avatar

So after lot of research, I finally decided to optimise the code by performing most time-consuming operations like querying data from the database in one transaction and then performing calculations with an optimised algorithm. I am getting the results in around 10 seconds.

adityar15's avatar

@chief_running_water thanks for sharing the link. I just used a walkaround for multithreading with some javascript and triggers. But this is something definitely to look on for future projects. Thanks for sharing it :)

Please or to participate in this conversation.