I have a few things to say:
First, you use recursion to process multiple rows. This can lead to problems if you become pregnant too deeply. Use a simple loop instead.
Secondly, did you know if this code can work several times? If a machine is connected to a CPU, it may not benefit from another thread. I suggest you manually check how many threads work best. More threads do not always speed up work, and in some cases it can slow everything down.
Finally, I would definitely put a limit on how many of these scripts can work at the same time. This can be achieved simply by the fact that each script runs for no more than 5 minutes. Or you can save the number of active scripts and make sure that it does not exceed the maximum number that you defined in the second sentence.
Edit: I added additional information about the recursion of the problem: Every time you call a function recursively, additional space is used on the stack. This space stores any local variables, as well as the address of the function (allowing it to restore state when the called function exits). The stack has only a finite amount of space, so, in the end, your program will crash with a stack overflow. Try running this simple program:
function a($i) { print $i . "\n"; a($i + 1); } a(0);
On my system, it aborts PHP after 608739 iterations. This number can be much smaller in a more complex function. A simple cycle does not have this overhead, so it does not have this problem.
source share