This article describes the multi-threaded concurrent implementation method of PHP. Share it for your reference, as follows:
Multi-threading in Java is a new thread thing. PHP relies on apache to rely on Linux to rely on a multi-threading method.
Here I will talk about how to simulate php concurrency if you cannot control the apache server
<?php if(function_exists('date_default_timezone_set')) { date_default_timezone_set('PRC'); } function a() { $time = time(); sleep(3); $fp = fopen('result_a'.$time.'.log', 'w'); fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); fclose($fp); } function b() { $time = time(); sleep(3); $fp = fopen('result_b'.$time.'.log', 'w'); fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); fclose($fp); } if(!isset($_GET['act'])) $_GET['act'] = 'a'; if($_GET['act'] == 'a') { a(); } else if($_GET['act'] == 'b') b(); ?>
The above code writes a file locally.
If you access localhost/ open both browser tags as fast as possible, you will find that the two files are created at 3 seconds
But if you access localhost/?act=b Another access/?act=a You find that the two files were created almost the same time.
For apache, the same url means a thread (we or a process), but different URLs means concurrent.
If there is a download action inside php
function runThread() { down("http://localhost/test/?act=a"); } if($_GET['act'] == 'run') { echo 'start:'; runThread(); echo ' End'; }
http://localhost/test/?act=run
http://localhost/test/?act=run&s=2
As long as the URL of the main access is different, it is considered to be different, which means concurrency. File creation time is not 3 seconds
Friends who have Linux servers locally can also use Linux to simulate concurrency
<?php for ($i=0;$i<10;$i++) { echo $i; sleep(5); } ?>
Save it above, and then write a SHELL code
#!/bin/bash for i in 1 2 3 4 5 6 7 8 9 10 do php -q & done
For more information about PHP related content, please check out the topic of this site:Summary of PHP process and thread operation skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of php sorting algorithm》、《Summary of common traversal algorithms and techniques for PHP》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of PHP mathematical operation skills》、《Summary of usage of php regular expressions》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.