2

My existing Laravel project is such that all the tasks are executed sequentially. I have identified part of the code which can be run in parallel using PHP threads and can reduce the response time.

So I'm trying to use pthreads, https://github.com/krakjoe/pthreads/tree/PHP5. The appserver.io is already comes with pthread, thus I'm running project on appserver.io and not apache.

I was able to use pthread successfully. As in creating new PDO connection under run() method for each thread and database interaction is working fine using native query like this:

self::$connection = new PDO ( 'mysql:host=127.0.0.1;dbname=mydb' , 'myuser' , 'mypass' );

But I'm not able to use Laravel's \DB:connection the same way. I need to have this working in order to run my code which is written with ORM based queries. How can I create new connection every time under run() method?

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279

2 Answers2

1

Can you not dispatch jobs and just have those jobs run in background (queue them)?

Ashit Vora
  • 2,902
  • 2
  • 27
  • 39
  • is this meant as a comment? – Gokigooooks Mar 25 '16 at 06:03
  • Thanks Ashit for answer. I think adding jobs top queue is good idea. But I guess queue will process them serially. Correct? That is what is happening right now (before multithreading) where every job is getting executed serially. I am hoping using multithreading, each thread can take up that function (which has all mysql queries), execute it (even if more cpu usage, I'm fine as I have good hardware), get results faster. – Kailash Gajara Mar 25 '16 at 06:13
1

Two things you need to consider. 1. You should load the vendor file in your threads. 2. Instead of facade use singleton class in threads.

Hope this will solve the issue.

satz
  • 113
  • 4
  • 13
  • 1
    @kailash Gajara i have been experimenting Laravel Artisan command using pthreads to push email. Here is the link [Pthreads+ Laravel](https://github.com/ssatz/PThreadsLaravel) . It might be useful. – satz May 17 '17 at 13:54