0

I have a PHP script "cron.php" to run by Task scheduler, which is on Windows 10 (XAMPP PHP 7.4.3). The script calls "process_1.php" and "process_2.php". As shown in code below, the 2nd process will run ONLY after the 1st process has completed.

Is there a way to run both processes concurrently?

In a browser, I could open two windows then launch 2 processes, and they would run separately and concurrently.

It seems the "include", "cURL", and "exec" all run two processes sequentially, as I tested below individually.

<?php
 // cron.php

 // 1st test
    include "process_1.php";
    include "process_2.php";

 // 2nd test
    exec (php.exe -f process_1.php);
    exec (php.exe -f process_1.php);

 // 3rd test
    $curl_obj = new curl_obj ();
    $curl_obj.exec (url='process_1.php');
    $curl_obj.exec (url='process_2.php');

?>

I could schedule 2 tasks to run "process_1.php" and "process_2.php" at the same time, to get exactly what I expect, in this way I don't even need "cron.php". But I am looking for a better solution. Otherwise, I will have to schedule 100 tasks if I have 100 processes. I hope one scheduled job could take care of all processes.

The benefit is time saving. Say each process takes 1 hour to complete, in cron job it will take 2 hours when running sequentially. If I could split them into 2 process running concurrently in scheduled job, it does not need two hours. When I had both processes running on separate browsers concurrently, it took 65 minutes to complete both.

So far I got to know following as possible solutions, and I will try them. Thank you for all responses!

  • Thread
  • Parallel
  • curl_multi_exec

Thank you!

trashycat
  • 1
  • 1
  • are you running all the processes 3 times? or these are only examples – Flash Thunder Nov 16 '22 at 16:50
  • `Is there a way to run both processes concurrently?`...maybe put them in two separate PHP scripts and have two separate scheduled tasks which trigger at the same time. `I am looking for a better solution`...define "better". `I will have to schedule 100 tasks if I have 100 processes`...or you'll have to write `exec` 100 times in your PHP code. Unless...you have a database with a list of the functions you want to run, and loop them. But realistically, your CPU can't run 100 jobs completely simultaneously anyway...what exactly are you all these 100 in-parallel processes supposed to be doing? – ADyson Nov 16 '22 at 17:18
  • @Flash Thunder The code shows how I tested 3 times by different methods. Thank you! – trashycat Nov 16 '22 at 18:26
  • @ADyson Sorry it was exaggerate to say 100 jobs. Let me say just two. As explanation added, I'd like to save time when both processes are running currently. Thank you! – trashycat Nov 16 '22 at 18:29
  • If you only have two, then by far the simplest way is to have two scheduled tasks. – ADyson Nov 16 '22 at 19:06
  • On windows it would be tricky, you can try this: https://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications , on linux you could use forks like in this answer-o-question: https://stackoverflow.com/questions/17526509/php-fork-limit-childs-in-the-task – Flash Thunder Nov 16 '22 at 19:44
  • @Flash Thunder Reading the url on windows, I am afraid it's probably not what I am looking for. Thank you! – trashycat Nov 17 '22 at 15:17
  • @Markus Zeller Thank you for heads up! Yeah, that is the correct direction. However, I was unable to make it work, for the "Class 'Thread' not found". Further research brought me *parallel*. Looking at the demo at https://github.com/krakjoe/parallel, which shows pound and dot printed alternately, I know that is exactly what I have been trying to get. – trashycat Nov 17 '22 at 16:18
  • 1
    @trashycat I gave exactly the same link that Markus Zeller did... :/ – Flash Thunder Nov 18 '22 at 12:03
  • @Flash Thunder My apologies! I opened quite several tabs on stackoverflow yesterday, and probably I mistakenly thought another URL was from you. You deserve the credit :-) – trashycat Nov 18 '22 at 16:04

0 Answers0