1

Hi so i am wondering if somebody can offer me a solution..

I have multiple instances of a php script i would like to run at once, I can semi achieve this using shell_exec() and not waiting for a response apart from the pid. This works wonderfully. However when doing this with the method below it works well for 100 or so executes but I need it to execute around 1000 times and it is taking too long to loop around the foreach and execute 1 by one.

Example:

 $items = $array(daatabase entries);

   foreach($items as $i){
    $pid = shell_exec("execute the process.php");
   }

Now assuming there are < 100 this will take around 1 second or so to exec which is OK however i need to run loads of these and this will take 10-20 seconds.

Is there any way of sending the commands in bulk rather than 1 at a time?

Thanks

6
  • do you expecting a response from your executed scripts ? Commented Mar 29, 2017 at 8:18
  • no I don't need the result - i already just get the pid back Commented Mar 29, 2017 at 8:28
  • how do you get the pid ? your php script ? Commented Mar 29, 2017 at 8:29
  • $pid = shell_exec("nohup nice -n 10 /usr/local/bin/php -c /opt/cpanel/ea-php71/root/etc/php.ini -f /home/USER/public_html/files/crons/test_process.php > /dev/null & echo $!") Commented Mar 29, 2017 at 8:34
  • well, you are sending your commands to background , so this is weird that iterating over 1000 rows takes all that time. Commented Mar 29, 2017 at 8:41

2 Answers 2

1

Something like pthreads perhaps?

https://www.sitepoint.com/parallel-programming-pthreads-php-fundamentals/ (The most recent result I could find)

Sign up to request clarification or add additional context in comments.

2 Comments

Not looked at pthreads yet, I have heard this is an option though
I don't believe it's core PHP but seems to be well established. The only alternative I've tried was to call an endpoint which carries out the action using curl with a short timeout. Exactly like this: stackoverflow.com/questions/35874707/…
0

You can execute process.php in background without waiting for execution completion. So it will be faster. Also shell_exec() function returns only output, not pid. If you need pid you have to use exec() function.

   $items = $array(daatabase entries);
   foreach($items as $i){
      shell_exec("execute the process.php > /dev/null 2>&1");
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.