(I read other questions, but they refer to older versions of PHP or frontend multi-threading)
I have a PHP/PostgreSQL application that has a complex backend processing part. Essentially, there is a very large loop (several thousand iterations) going over the same data again and again (with permutations). In each loop, the same data is read, operations are applied, the result is written back to the database. The loops are entirely independent from each other, no results are kept between loops. In fact, to clear the object cache memory (using Doctrine), I clear out the cache every 100 or so loops.
So I essentially have:
for ($i=0; $i<5000; $i++) {
// fetch data
// manipulate data
// write results to a different table
}
The original data is never touched during these loops, only several results tables are populated.
This currently takes several minutes. I seems to me like a textbook example of parallel processing.
What is the best way to put this into multiple threats? I don't care much about execution order or even if the workload is distributed evenly (by nature of the data operations, if all threads run the same number of loops, they should end up with more or less the same workload). All I want is to use more of my CPU cores.
I've done multi-threading in PHP 5 and it was... well... not perfect. Workable, but difficult. Has this improved in PHP 7 ? Is there a relatively simple way to basically say "for (...) and run it in n threads" ?
In case it matters, the app is written in Symfony4 and this backend process is called via a console command.