forking multiple processes at a time

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SomeGei

    forking multiple processes at a time

    i have a php script that needs to execute an external php script (call this
    script 2) when the user clicks a link, however I don't want the user to have
    to wait until script 2 finishes executing before he sees the output of
    script one... i know I can use the exec() command to do this, and use the &
    to run the command in the background....

    but say i want to run 100 instances of script 2 when the user clicks the
    link (with different arguments)... i dont want to have all 100 processes
    running at the same time... what I would like to do is run 10 or so at once,
    and once those 10 are completed, run another 10...

    how would i accomplish this?


  • Dani CS

    #2
    Re: forking multiple processes at a time

    SomeGei wrote:[color=blue]
    > i have a php script that needs to execute an external php script (call this
    > script 2) when the user clicks a link, however I don't want the user to have
    > to wait until script 2 finishes executing before he sees the output of
    > script one... i know I can use the exec() command to do this, and use the &
    > to run the command in the background....
    >
    > but say i want to run 100 instances of script 2 when the user clicks the
    > link (with different arguments)... i dont want to have all 100 processes
    > running at the same time... what I would like to do is run 10 or so at once,
    > and once those 10 are completed, run another 10...
    >
    > how would i accomplish this?[/color]

    Could the last process of each batch launch the next 10 processes?

    Another approach is using cron and having script 1 store in a file the
    arguments for the 100 instances of script 2. cron would run a "launcher"
    every minute, and this launcher would execute a copy of script 2 for
    each set of arguments in the file.

    Comment

    • Pedro Graca

      #3
      Re: forking multiple processes at a time

      SomeGei wrote:[color=blue]
      > i dont want to have all 100 processes running at the same time...
      > what I would like to do is run 10 or so at once,
      > and once those 10 are completed, run another 10...
      >
      > how would i accomplish this?[/color]

      Check out popen() and related functions




      <?php // basic code with no error-checking
      for ($i=0; $i<10; ++$i) {

      /* open ten processes */
      for ($j=0; $j<10; ++$j) {
      $pipe[$j] = popen('script2. php', 'w');
      }

      /* wait for them to finish */
      for ($j=0; $j<10; ++$j) {
      pclose($pipe[$j]);
      }
      }
      ?>
      --
      Mail to my "From:" address is readable by all at http://www.dodgeit.com/
      == ** ## !! ------------------------------------------------ !! ## ** ==
      TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
      may bypass my spam filter. If it does, I may reply from another address!

      Comment

      • Chung Leong

        #4
        Re: forking multiple processes at a time


        "SomeGei" <some@gei.com > wrote in message
        news:qJudnUpNpr 0n50ncRVn-rQ@giganews.com ...[color=blue]
        > i have a php script that needs to execute an external php script (call[/color]
        this[color=blue]
        > script 2) when the user clicks a link, however I don't want the user to[/color]
        have[color=blue]
        > to wait until script 2 finishes executing before he sees the output of
        > script one... i know I can use the exec() command to do this, and use the[/color]
        &[color=blue]
        > to run the command in the background....
        >
        > but say i want to run 100 instances of script 2 when the user clicks the
        > link (with different arguments)... i dont want to have all 100 processes
        > running at the same time... what I would like to do is run 10 or so at[/color]
        once,[color=blue]
        > and once those 10 are completed, run another 10...
        >
        > how would i accomplish this?
        >
        >[/color]

        Have each instance of the program spawn a copy of itself when it finishes.


        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: forking multiple processes at a time

          SomeGei wrote:[color=blue]
          > i have a php script that needs to execute an external php script[/color]
          (call this[color=blue]
          > script 2) when the user clicks a link, however I don't want the user[/color]
          to have[color=blue]
          > to wait until script 2 finishes executing before he sees the output[/color]
          of[color=blue]
          > script one... i know I can use the exec() command to do this, and use[/color]
          the &[color=blue]
          > to run the command in the background....[/color]
          <snip>

          Execute an external program and display the output

          <quote>
          Note: If you start a program using this function and want to leave it
          running in the background, you have to make sure that the output of
          that program is redirected to a file or some other output stream or
          else PHP will hang until the execution of the program ends.
          </quote>

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          Working...