Try PHP command line scripting Multi-process concurrency execution

Source: Internet
Author: User
Tags fread vars

PHP does not support multi-threading, but we can convert the problem to "multi-process" to solve. Because Pcntl_fork in PHP is available only on UNIX platforms, this article attempts to use Popen instead.

Here is an example:

Subroutines that are called in parallel:

  1. <?php
  2. if ($argc ==1) {
  3. Echo ("argv\n");
  4. }
  5. $arg = $argv [1];
  6. for ($i = 0; $i <10; $i + +)
  7. {
  8. Echo ($i.". 1.". Time (). "exec $arg \ n");
  9. if ($arg = =' PhP2 ')
  10. {
  11. Sleep (1);
  12. Echo ($i.". 2.". Time (). "exec $arg \ n");
  13. Sleep (1);
  14. }else{
  15. Sleep (1);
  16. }
  17. ?>

----------------------------
The main caller program, by which he invokes the child process, concurrently collects the output of the subroutine

  1. <?php
  2. Error_reporting (E_all);
  3. $handle 1 = popen (' php sub.php php1 ', ' R ');
  4. $handle 2 = popen (' php sub.php php2 ', ' R ');
  5. $handle 3 = popen (' php sub.php php3 ', ' R ');
  6. echo "' $handle 1 ';". GetType ($handle 1). "\ n";
  7. echo "' $handle 2 ';". GetType ($handle 2). "\ n";
  8. echo "' $handle 3 ';". GetType ($handle 3). "\ n";
  9. Sleep (20);
  10. while (! Feof ($handle 1) | |! Feof ($handle 2) | |! Feof ($handle 3)) {
  11. $read = fgets ($handle 1);
  12. echo $read;
  13. $read = fgets ($handle 2);
  14. echo $read;
  15. $read = fgets ($handle 3);
  16. echo $read;
  17. }
  18. Pclose ($handle 1);
  19. Pclose ($handle 2);
  20. Pclose ($handle 3);
  21. ?>

-------------------

Here is the output on my machine:


> PHP exec.php
' Resource ID #4 '; Resource
' Resource ID #5 '; Resource
' Resource ID #6 '; Resource
0.1.1147935331 exec Php1
0.1.1147935331 exec PhP2
0.1.1147935331 exec php3
1.1.1147935332 exec Php1
0.2.1147935332 exec PhP2
1.1.1147935332 exec php3
2.1.1147935333 exec Php1
1.1.1147935333 exec PhP2
2.1.1147935333 exec php3
3.1.1147935334 exec Php1
1.2.1147935334 exec PhP2
3.1.1147935334 exec php3
4.1.1147935335 exec Php1
2.1.1147935335 exec PhP2
4.1.1147935335 exec php3
5.1.1147935336 exec Php1
2.2.1147935336 exec PhP2
5.1.1147935336 exec php3
6.1.1147935337 exec Php1
3.1.1147935337 exec PhP2
6.1.1147935337 exec php3
7.1.1147935338 exec Php1
3.2.1147935338 exec PhP2
7.1.1147935338 exec php3
8.1.1147935339 exec Php1
4.1.1147935339 exec PhP2
8.1.1147935339 exec php3
9.1.1147935340 exec Php1
4.2.1147935340 exec PhP2
9.1.1147935340 exec php3
5.1.1147935341 exec PhP2
5.2.1147935342 exec PhP2
6.1.1147935343 exec PhP2
6.2.1147935344 exec PhP2
7.1.1147935345 exec PhP2
7.2.1147935346 exec PhP2
8.1.1147935347 exec PhP2
8.2.1147935348 exec PhP2
9.1.1147935349 exec PhP2
9.2.1147935350 exec PhP2

* * Summary: * *

* * The main program loop waits for the sub-process, through the output of the fgets or fread processing process, from the timestamp, it is true that concurrent execution. **

-----------------------------------------------
Improved:

* Popen Open handles are unidirectional, and if you need to interact with child processes, you can use Proc_open
* Use arrays and sub-functions instead of while (!feof ($handle 1) | |!feof ($handle 2) | |!feof ($handle 3)) This sordid writing
* Use fread once the output of the process has been generated, rather than each row.


This is another improvement:
A dispatcher who executes the shell task concurrently, this program reads a task file, executes each line of command concurrently, and can set the number of simultaneous child processes:

  1. <?
  2. /*
  3. Executive Manager
  4. Concurrent Execution sub-task list
  5. */
  6. Include (".. /common/conf.php ");
  7. Include (".. /common/function.php ");
  8. Number of open processes
  9. $exec _number = 40;
  10. /***** Main ********/
  11. if ($argc ==1) {
  12. Echo ("argv\n");
  13. }
  14. $taskfile = $argv [1];
  15. Tasklist
  16. $tasklist = File ($taskfile);
  17. $tasklist _len = count ($tasklist);
  18. $tasklist _pos = 0;
  19. $handle _list = Array ();
  20. while (1) {
  21. //Child process list is free, the list of child processes is populated
  22. if ($exec _number > count ($handle _list) &&
  23. $tasklist _pos < $tasklist _len)
  24. {
  25. For ($i =$tasklist _pos; $i <$tasklist _len;)
  26. {
  27. $command = $tasklist [$i];
  28. $handle _list[] = Popen ($command, "R");
  29. Tolog ("begin task \ t".  $tasklist [$i]);
  30. $i + +;
  31. if ($exec _number = = count ($handle _list)) break ;
  32. }
  33. $tasklist _pos = $i;
  34. }
  35. //If the child process list is empty, exit
  36. if (0 = = count ($handle _list))
  37. {
  38. Break ;
  39. }
  40. //Check the output of the sub-process list, close and record the disabled sub-processes
  41. $end _handle_keys = Array ();
  42. foreach ($handle _list as $key = $handle)
  43. {
  44. //$str = Fgets ($handle, 65536);
  45. $str = fread ($handle, 65536);
  46. Echo ($str);
  47. if (feof ($handle))
  48. {
  49. $end _handle_keys[] = $key;
  50. Pclose ($handle);
  51. }
  52. }
  53. //Kick out the stopped sub-process
  54. foreach ($end _handle_keys as $key)
  55. {
  56. unset ($handle _list[$key]);
  57. //var_dump ($handle _list);
  58. //exit;
  59. }
  60. }
  61. Tolog ("\n\n*******************end**********************\n\n", "", true);
  62. ?>

Try PHP command line scripting Multi-process concurrency execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.