5

Possible Duplicate:
php execute a background process
how can I achieve a task that should be done in thread in php

I have this PHP script that takes a little long to process. However I don't want the viewer to wait for the script to finish. I want him to be able to continue browsing and/or close the browser. Yet I want the script to continue to work... How can I do this? Is there way to create some sort of thread that will allow such a thing?

p.s. I really don't want to re-write the script in another language and execute it via os.

Community
  • 1
  • 1
JohnRoach
  • 747
  • 4
  • 11
  • 26

5 Answers5

10

If you can run your script from the CLI, you can "fork" it into a background process from the script your web server is serving by calling something like:

exec('php your_script.php > /dev/null &');

This will allow the page to finish loading, but keep your_script.php running.

nnevala
  • 5,779
  • 2
  • 20
  • 13
  • Can I use $_GET to send data to my script via your way? A sample would be : exec('php my_script.php?somevalues > /dev/null &') – JohnRoach Nov 16 '11 at 08:24
  • 1
    @JohnRoach `exec` will run a command in the operating system. So you have to pass parameters that way, there is no HTTP involved here (GET, POST, etc. are HTTP methods). [PHP: Command line usage](http://php.net/manual/en/features.commandline.php) – kapa Nov 16 '11 at 08:27
7

Avoid the idea of running paralell tasks (threads or processes) in PHP. It has some modules for this purpose, but they are not available on every platform and are not part of the PHP core, so the chances for you to be able to use them are very low.

You have two options here:

  1. Split the long running task into several (cheaper) sub-tasks. Put them on a queue and make each request execute some of these sub-tasks (but don't delay the output to the client too much).

  2. If you can't split this big task, just setup a cron job for it. This way users are unnafected by the delay as the long running task is execute by the system.

    Another thing to note is that PHP script have (an overridable) time limit that halts execution after a given amount of seconds. Tasks that run via cron don't suffer this limit.

Community
  • 1
  • 1
azkotoki
  • 2,357
  • 2
  • 22
  • 26
2

As far as I know PHP doesn't offer anything to perform such tasks.

One working(though ugly) solution is to create SOAP web service and invoke it from your script with extended SoapClient by overriding "$oneWay" parameter (http://www.php.net/manual/en/soapclient.dorequest.php).

    class AsyncSoapClient extends SoapClient {
      function __construct($wsdl, $options) {
        parent::__construct($wsdl, $options);
      }

      function __doRequest($request, $location, $action, $version, $oneWay = TRUE) {
        return parent::__doRequest($request, $location, $action, $version, TRUE);
      }
    }

    $client = new AsyncSoapClient(
      NULL,
      array(
        'location' => $location,
        'uri'      => $uri
      )
    );

    $client->invokeMethodOnServer($yourParameters);

In this case making one way request means that your script doesn't need to wait for response and execution of your code will continue.

ioseb
  • 16,625
  • 3
  • 33
  • 29
  • You said it was an ugly way why is that? It seems your solution is exactly what I want. – JohnRoach Nov 16 '11 at 11:27
  • It would be more natural to use event pool or different thread for such purposes. This solution works though you need to create SOAP server which accepts request from client, implement oneway SOAP client etc... – ioseb Nov 16 '11 at 11:34
1

You can make use of ajax ,

call the script that takes long time to execute via ajax , and when the result is ready , show it to user

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48
  • 1
    User can navigate to a different page or close browser, then the script execution will be interrupted/lost – Remus Rusanu Nov 16 '11 at 08:12
  • Exactly... Which is why I posted my question on stackoverflow... – JohnRoach Nov 16 '11 at 08:13
  • @VladislavZorov: because AJAX (JS) runs on the client. The user may interrupt the call (or have JS disabled altogether), the request may time out if the operation is long, etc. And, the question was not about AJAX, but being able to run a PHP operation in the background. The two is not to be confused. – kraxor May 26 '14 at 10:53
  • 1
    That was me from the past, ignore that guy. – Vladislav Zorov Jun 06 '14 at 21:32
  • 1
    PHP is not necessarily used on websites. For example I'm using PHP to build a Telegram Bot, so your answer cannot be applied for this purpose. – Marco Concas Feb 20 '20 at 14:36
1

OK if so u can do another thing , u can maintain a table to record the status of the script execution , then write an JavaScript code that do ajax call to check the recorded status , and if its true then retrieve the data and then show it to the user ..

Akhil Thayyil
  • 9,263
  • 6
  • 34
  • 48