PHP multi-threaded programming implementation and non-blocking implementation. PHP does not support multi-threading by default. To use multi-threading, you need to install the pthread extension. After installation, you can perform multi-thread programming.
Thread concept
Thread is the smallest unit that the operating system can schedule
A multi-threaded program is more likely to be scheduled by the operating system than a single-threaded program Larger, so multi-threaded programs are generally more efficient than single-threaded programs;
Multiple threads of a multi-threaded program can run on multiple cores of a multi-core CPU at the same time, taking full advantage of the multi-core machine;
The system overhead of creating and switching threads is smaller than that of processes, so it is more efficient than multiple processes to a certain extent;
Threads are born with shared memory space, and communication between threads is simpler. Avoids the introduction of new complexity by process IPC.
When to use threads
I/O blocking will cause the operating system to schedule tasks and block the current task, so when there is a lot of I/O in the code, When using multi-threading, the code can be parallelized
Multi-threading can make full use of the CPU, so when there are multiple large calculation codes, you can also use multi-threading to execute them in parallel
Use The good and bad of threads
The thread safety implemented by PHP mainly uses the TSRM mechanism to isolate global variables and static variables, and copies global variables and static variables to each thread. Each thread uses a backup of the main thread, thus avoiding variable conflicts and thread safety issues.
Once the sub-thread starts running, the main thread can no longer adjust the details of the sub-thread running
Extended installation
PHP does not support multiple by default Threads. To use multi-threading, you need to install the pthread extension. To install the pthread extension, you must use the --enable-maintainer-zts parameter to recompile PHP. This parameter specifies the thread safety method when compiling PHP.
./configure --enable-maintainer-zts --with-tsrm-pthreads
Example
class Request extends Thread { public $url; public $response; public function __construct($url) { $this->url = $url; } public function run() { $this->response = file_get_contents($this->url); } } $chG = new Request("www.google.com"); $chB = new Request("www.baidu.com"); $chG ->start(); $chB ->start(); $chG->join(); $chB->join(); $gl = $chG->response; $bd = $chB->response;
PHP non-blocking
Use fastcgi_finish_request()
echo "program start..."; fastcgi_finish_request(); sleep(1);echo 'debug1...'; sleep(10);echo 'debug2...';
Use fsockopen( )
stream_set_blocking()
Use cURL
$cmh = curl_multi_init();
Use Gearman/Swoole extension
Use cache and queue
redis
Calling system commands
$cmd = 'nohup php ./processd.php $someVar >/dev/null &';
Use pcntl_fork()
PHP native support
yield
Related recommendations:
php install threads multi-thread extension, phpthreads multi-thread
The above is the detailed content of PHP implements non-blocking and multi-threaded programming. For more information, please follow other related articles on the PHP Chinese website!