I have a huge amount of data that I want to process in fast and get most out of my server. So, I have created one parent artisan command and one child artisan command.
Parent artisan command: Gets the value from the database, does all kind of calculations, divides data into chunks and executes multiple instances of child artisan command using the following code.
foreach($arrayOfData as $chunk){ // $arrayOfData is an array of Chunks
$this->call('process:data',[
'data' => $chunk // Chunk is an array containing data
]);
$this->info('Chunk Processed');
}
But it waits for one instance to stop execution which means it is not executing multiple instances of child artisan commands at once which I want! I know that calling it this way will wait for one instance of child artisan command to stop the execution. Is there any way I can execute multiple instances of child artisan command
without waiting for one instance to stop the execution?
I already searched for different ways to do this, I can execute child artisan commands using exec()
and similar functions but it will also return some exit code for which my code will wait for.
By Executing just 2 instances at once, I can divide time taken to process data by 2. And I want to execute 3 instances at once! Is there any way to do this?
More information
Child artisan command: Gets an array of data and processes it. Basically, it updates some information in the database for the given data.
From the docs:
Using the
queue
method on the Artisan facade, you may even queue Artisan commands so they are processed in the background by your queue workers. Before using this method, make sure you have configured your queue and are running a queue listener
foreach($arrayOfData as $chunk){ // $arrayOfData is an array of Chunks
$this->queue('process:data',[
'data' => $chunk // Chunk is an array containing data
]);
}
If you're not familiar with queues, start from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With