Possible to run a PHP function in background?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    Possible to run a PHP function in background?

    Hi,

    I'm using PHP 5. I have a function that takes a little while to run,
    and I don't want the user to have to wait while it does. Is there a
    way to run this function in the background?

    Thanks for your help, - Dave
  • Jerry Stuckle

    #2
    Re: Possible to run a PHP function in background?

    laredotornado@z ipmail.com wrote:
    Hi,
    >
    I'm using PHP 5. I have a function that takes a little while to run,
    and I don't want the user to have to wait while it does. Is there a
    way to run this function in the background?
    >
    Thanks for your help, - Dave
    >
    Within the web server environment, not really. But you can start a CLI
    job to do the work if your hosting company allows them. Or you can
    queue up the request (i.e. in a database) and have a cron job run
    regularly to handle the queue.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • EGreg

      #3
      Re: Possible to run a PHP function in background?

      On Jul 28, 10:56 pm, "laredotorn...@ zipmail.com"
      <laredotorn...@ zipmail.comwrot e:
      Hi,
      >
      I'm using PHP 5. I have a function that takes a little while to run,
      and I don't want the user to have to wait while it does. Is there a
      way to run this function in the background?
      >
      Thanks for your help, - Dave
      Well, there are many ways you can run a function asynchronously (i.e.
      not wait until it returns). One way is to fork the process. Another
      way is to call some asynchronous function exposed by a PHP extension,
      but then how will you get its result? You might have to call the
      function again from another script, to collect the return value. The
      second time around, it should BLOCK on a semaphore or better yet, on a
      message queue. Look those up.

      Comment

      • DS

        #4
        Re: Possible to run a PHP function in background?

        On 2008-07-28 19:56:49 -0700, "laredotornado@ zipmail.com"
        <laredotornado@ zipmail.comsaid :
        Hi,
        >
        I'm using PHP 5. I have a function that takes a little while to run,
        and I don't want the user to have to wait while it does. Is there a
        way to run this function in the background?
        >
        Thanks for your help, - Dave
        We had a similar problem, and our large job took so much time, we split
        it up into 10 background processes which filtered URLs in a large
        database -- each of the 10 processes were operating on 1/10th of the
        records in the main table. So this is what we did.

        Note sending output to '/dev/null' surpressed any output from the
        process; results from the PHP script were written to another database
        table.

        The ampersand at the end & meant to run the process in the background
        (as a lower priority than foreground processes.)

        Of course this is an approach that can be used if you are using
        colocation or a dedicated server; if you use shared hosting, your ISP
        may limit the use of the PHP exec () command:

        $pathToPHPbinar y = '/u01/apps/php/5.1.4/bin/php';
        $pathToScriptTo Spawn = '/u01/apps/www/runTests/siteFilter.php' ;

        $limit = 10;
        for ($i=1; $i<=$limit; ++$i)
        {
        exec ("$pathToPHPbin ary $pathToScriptTo Spawn $i $limit >/dev/null &");
        }


        Comment

        • Hans-Werner Hilse

          #5
          Re: Possible to run a PHP function in background?

          Hi,

          DS <ds@noSpam.comw rote:
          We had a similar problem, and our large job took so much time, we
          split it up into 10 background processes which filtered URLs in a
          large database -- each of the 10 processes were operating on 1/10th
          of the records in the main table. So this is what we did.
          Long running job's shouldn't be run by a webserver SAPI PHP. One really
          should use CLI for that. It really messes up the webserver's resource
          management otherwise and there is no good reason. Oh, looking further
          down, you seem to do that.

          Running parallel processes might make sense if the database doesn't
          utilize available processor cores by itself in a senseful way.
          The ampersand at the end & meant to run the process in the background
          (as a lower priority than foreground processes.)
          No, it should have the same priority. You'd need to »nice« the process
          to give a different priority. It is in the »background«, however. But
          it'll most certainly die if the shell dies... Use »nohup« to prevent
          that.


          Of course, in such scenarios, extra care is needed for proper locking,
          at least, if data is modified.


          -hwh

          Comment

          • Olaf Schinkel

            #6
            Re: Possible to run a PHP function in background?

            laredotornado@z ipmail.com schrieb:
            Hi,
            >
            I'm using PHP 5. I have a function that takes a little while to run,
            and I don't want the user to have to wait while it does. Is there a
            way to run this function in the background?
            >
            Thanks for your help, - Dave
            You can use AJAX.

            Display the main-Screen, send the 10 request via AJAX to the server and
            the user can go ahead.

            Comment

            • Hans-Werner Hilse

              #7
              Re: Possible to run a PHP function in background?

              Hi,

              Olaf Schinkel wrote:
              laredotornado@z ipmail.com schrieb:
              Hi,

              I'm using PHP 5. I have a function that takes a little while to
              run, and I don't want the user to have to wait while it does. Is
              there a way to run this function in the background?

              Thanks for your help, - Dave
              You can use AJAX.

              Display the main-Screen, send the 10 request via AJAX to the server
              and the user can go ahead.
              Those 10 connections would be aborted/cut if the user actually does go
              ahead. The Javascript context would be ripped out. Except, of course,
              if you never go to a new page and »going ahead« is rather meant as
              using AJAX for that, too.

              But it is really a design problem to run long-running (»batch«-style)
              tasks by utilizing a web server for process control.


              -hwh

              Comment

              • Olaf Schinkel

                #8
                Re: Possible to run a PHP function in background?

                Hans-Werner Hilse schrieb:
                Hi,
                >
                Olaf Schinkel wrote:
                >
                >laredotornado@z ipmail.com schrieb:
                >>Hi,
                >>>
                >>I'm using PHP 5. I have a function that takes a little while to
                >>run, and I don't want the user to have to wait while it does. Is
                >>there a way to run this function in the background?
                >>>
                >>Thanks for your help, - Dave
                >You can use AJAX.
                >>
                >Display the main-Screen, send the 10 request via AJAX to the server
                >and the user can go ahead.
                >
                Those 10 connections would be aborted/cut if the user actually does go
                ahead. The Javascript context would be ripped out. Except, of course,
                if you never go to a new page and »going ahead« is rather meant as
                using AJAX for that, too.
                >
                But it is really a design problem to run long-running (»batch«-style)
                tasks by utilizing a web server for process control.
                >
                Yes. It is a design problem.
                And....PHP is slow.
                I write some programs in C++, when i have such problems.

                Comment

                • Erwin Moller

                  #9
                  Re: Possible to run a PHP function in background?

                  Olaf Schinkel schreef:
                  laredotornado@z ipmail.com schrieb:
                  >Hi,
                  >>
                  >I'm using PHP 5. I have a function that takes a little while to run,
                  >and I don't want the user to have to wait while it does. Is there a
                  >way to run this function in the background?
                  >>
                  >Thanks for your help, - Dave
                  You can use AJAX.
                  >
                  Display the main-Screen, send the 10 request via AJAX to the server and
                  the user can go ahead.
                  Hi Olaf,

                  Using Ajax for such a task is Bad Idea I think.
                  Using Ajax to call 10 scripts adds the client (=browser) into your chain
                  of logic, which makes your app unreliable.
                  What is user goes away just before your Ajax is executed?
                  What happens if JavaScript is not on?

                  If those 10 processes are important for the app, you want to make sure
                  they get executed. Putting the browser of the client in command of that
                  is dangerous and unreliable. It is wiser to let the script take care of it.

                  Regards,
                  Erwin Moller

                  Comment

                  • binaryjesus

                    #10
                    Re: Possible to run a PHP function in background?

                    On Jul 29, 7:56 am, "laredotorn...@ zipmail.com"
                    <laredotorn...@ zipmail.comwrot e:
                    Hi,
                    >
                    I'm using PHP 5. I have a function that takes a little while to run,
                    and I don't want the user to have to wait while it does. Is there a
                    way to run this function in the background?
                    >
                    Thanks for your help, - Dave
                    i am taking your "little-while" to be less than 5 mins and also
                    guessing that ur on a linux shared host.

                    what u can do is exec("wget -b ". "http://site/put_heavy_func_ here.php?
                    all_the_data" );
                    -b will put webget in the background amd exec would return
                    immediately.

                    this is almost like ajax but much much better as its bound to be
                    called by the server it self which is what u want. nad u can get the
                    result through akax if the need be.


                    in case ur denied access to wget (didnt mention its a linux
                    downloader) or on a windows serer than u can use
                    $dump = get_file_conten t("http://link.here") curl is always there as a
                    backup
                    hope this helps

                    binaryjesus

                    Comment

                    • laredotornado@zipmail.com

                      #11
                      Re: Possible to run a PHP function in background?

                      On Jul 29, 8:22 am, binaryjesus <coolman.gu...@ gmail.comwrote:
                      On Jul 29, 7:56 am, "laredotorn...@ zipmail.com"
                      >
                      <laredotorn...@ zipmail.comwrot e:
                      Hi,
                      >
                      I'm using PHP 5.  I have a function that takes a little while to run,
                      and I don't want the user to have to wait while it does.  Is there a
                      way to run this function in the background?
                      >
                      Thanks for your help, - Dave
                      >
                      i am taking your "little-while" to be less than 5 mins and also
                      guessing that ur on a linux shared host.
                      >
                      what u can do is exec("wget -b ". "http://site/put_heavy_func_ here.php?
                      all_the_data" );
                      -b will put webget in the background amd exec would return
                      immediately.
                      >
                      this is almost like ajax but much much better as its bound to be
                      called by the server it self which is what u want. nad u can get the
                      result through akax if the need be.
                      >
                      in case ur denied access to wget (didnt mention its a linux
                      downloader) or on a windows serer than u can use
                      $dump = get_file_conten t("http://link.here") curl is always there as a
                      backup
                      hope this helps
                      >
                      binaryjesus
                      Thanks all for your answers. binaryjesus has the solution that is
                      easiest for me to implement. Also, great handle! - Dave

                      Comment

                      Working...