Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>PHP doesn't support concurrency in core. But you can simulate a similar behaviour with the following methods.</p> <p><strong>1. fsockopen() and ignore_user_abort('1')</strong></p> <p>You can create a socket connection to another script which runs then in background. Please see the following two scripts. The main script creates a socket connection to the sub script and closes it immediately. The sub script runs until all tasks are finished. This is because of the ini value "ignore_user_abort" is set to 1.</p> <p>main.php</p> <pre><code>&lt;?php $start = microtime(true); $host = 'localhost'; $target = '/sub.php'; $fp = fsockopen($host, 80); $request = "GET $target HTTP/1.1\r\n"; $request .= "Host: $host\r\n"; $request .= "Connection: Close\r\n\r\n"; fwrite($fp, $request); fclose($fp); file_put_contents("main", "Script runs " . (microtime(true) - $start) . " seconds"); </code></pre> <p>sub.php</p> <pre><code>&lt;?php $start = microtime(true); ignore_user_abort("1"); sleep(5); file_put_contents("sub", "Script runs " . (microtime(true) - $start) . " seconds"); </code></pre> <hr> <p><strong>2. Start a script over the command line in background</strong> </p> <p>You can start a script over the unix/linux or windows command line in background. The following example is for the unix/linux command line. To start the script over the windows command line see the following comment on the PHP.net site. <a href="http://www.php.net/manual/en/function.exec.php#110131" rel="nofollow">http://www.php.net/manual/en/function.exec.php#110131</a></p> <p>main.php</p> <pre><code>&lt;?php $start = microtime(true); pclose(popen("/usr/bin/php " . __DIR__ . "/sub.php " . __DIR__ . "//sub.log 2&gt;&amp;1 &amp;", 'r')); file_put_contents("main", "Script runs " . (microtime(true) - $start) . " seconds"); </code></pre> <p>sub.php</p> <pre><code>&lt;?php $start = microtime(true); sleep(5); file_put_contents("sub", "Script runs " . (microtime(true) - $start) . " seconds"); </code></pre> <hr> <p><strong>The following extension provides real concurrency in PHP:</strong></p> <blockquote> <p>pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Stackables. </p> </blockquote> <p><a href="http://www.php.net/manual/en/book.pthreads.php" rel="nofollow">http://www.php.net/manual/en/book.pthreads.php</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload