An in-depth analysis of (pseudo) multi-threading and multi-processing in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:01:45
Original
779 people have browsed it

(pseudo) multi-threading: With the help of external force
Use the multi-threading of the WEB server itself for processing, and call the program we need to implement multi-threading multiple times from the WEB server.
QUOTE:
We know that PHP itself does not support multi-threading, but our WEB server does support multi-threading.
That is to say, multiple people can access it at the same time. This is also the basis for me to implement multi-threading in PHP.
Suppose we are running the file a.php. But in the program, I also request the WEB server to run another b.php
Then these two files will be executed simultaneously.
(PS: After a link request is sent, the WEB server will execute it, regardless of whether the client has exited)
Sometimes, what we want to run is not Another file, but a part of the code in this file. What should I do?
In fact, it is through parameters to control which program a.php runs.
Let’s look at an example:

Copy code The code is as follows:

function runThread(){
$fp = fsockopen('localhost', 80, $errno, $errmsg);
fputs($fp, "GET /a.php?act=brnrn");//The second parameter here is specified in the HTTP protocol Request header, if you don’t understand, please see the definition in RFC           );
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn");
fclose($fp) ; i:s', time()) . (double)microtime() . "rn");
fclose($fp);
}
if(!isset($_GET['act'] )){ $_GET['act'] = 'a';};
if($_GET['act'] == 'a'){
runThread();
a();
}else if($_GET['act'] == 'b'){
b();
};
?>


Open result_a.log and result_b.log and compare the access times of the two files. You will find that these two are indeed running in different threads. Some times are exactly the same.
The above is just a simple Example, you can improve it into other forms.
Since PHP can also have multi-threading, then the problem has arisen, that is, the problem of synchronization. We know that PHP itself does not support multi-threading. So there will be no What is it like synchronize method in Java. So how should we do it.

1. Try not to access the same resource to avoid conflicts. But you can operate the database at the same time. Because the database supports concurrent operations, so in multi-threaded PHP
do not access the same resource. Write data to a file. If you must write, use other methods for synchronization. For example, call flock to lock the file. Or create a temporary file and wait for the file to disappear in another thread while(file_exits( 'xxx')); This means that when this temporary file exists, it means that the thread is actually operating. If the file does not exist, it means that other threads have released this.

2. Try not to read data from the socket that runThread takes after executing fputs. Because to achieve multi-threading, it is necessary to use non-blocking mode. That is, return immediately when functions like fgets are executed .. So there will be problems when reading and writing data. If blocking mode is used, the program is not multi-threaded. It has to wait for the return from above before executing the following program. So if data needs to be exchanged, it can be completed using external files or data. . If you really want it, use socket_set_nonblock($fp) to achieve it.

Having said so much, does this have any practical significance? When is it necessary to use this method?
The answer is yes. As we all know, in an application that constantly reads network resources, the speed of the network is the bottleneck. If you adopt this method, you can read different pages with multiple threads at the same time.

I made a program that can search for information from shopping mall websites such as 8848 and soaso. There is also a program that reads business information and company directories from the Alibaba website and also uses this technology. Because both programs have to continuously connect to their servers to read information and save it to the database. Utilizing this technology eliminates the bottleneck of waiting for a response.

Multiple processes: Use PHP's Process Control Functions (PCNTL/Thread Control Function)
Can only be used on Unix Like OS, not available on Windows.
When compiling php, you need to add --enable-pcntl, and it is recommended to only run in CLI mode, not in a WEB server environment.
The following is a short test code:

Copy the code The code is as follows:

declare( ticks=1);
$bWaitFlag = FALSE; /// Whether to wait for the process to end
$intNum = 10; 🎜>echo ("Startn");
for($i = 0; $i < $intNum; $i++) {
$pids[$i] = pcntl_fork();/// Generate child process , and start the trial run code from the current line, and do not inherit the data information of the parent process
if(!$pids[$i]) {
// Child process code segment_Start
$ str="";
sleep(5+$i);
for ($j=0;$j<$i;$j++) {$str.="*";}
echo " $i -> " . time() . " $str n";
exit();
// Subprocess process code segment_End
}
}
if ($ bWaitFlag)
{
for($i = 0; $i < $intNum; $i++) {
pcntl_waitpid($pids[$i], $status, WUNTRACED);
echo " wait $i -> " . time() . "n";
}
}
echo ("Endn");


运行结果如下:
CODE:[Copy toclipboard][qiao@oicq qiao]$ phptest.php       
Start
End
[qiao@oicq qiao]$ ps -aux | grep "php"
qiao     32275  0.0  0.5 49668 6148pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32276  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32277  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32278  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32279  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32280  0.0  0.5 49668 6152pts/1    S    14:03   0:00 /usr/local/php4/b
qiao     32281  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32282  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32283  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32284  0.0  0.5 49668 6152pts/1    S    14:03   0:00/usr/local/php4/b
qiao     32286  0.0  0.0  1620  600pts/1    S    14:03   0:00 grep php
[qiao@oicq qiao]$ 0 -> 1133503401 
1 -> 1133503402 *
2 -> 1133503403 **
3 -> 1133503404 ***
4 -> 1133503405 ****
5 -> 1133503406 *****
6 -> 1133503407 ******
7 -> 1133503408 *******
8 -> 1133503409 ********
9 -> 1133503410 *********
[qiao@oicq qiao]$
如果$bWaitFlag=TURE,则结果如下:
CODE:[Copy toclipboard][qiao@oicq qiao]$ phptest.php       
Start
0 -> 1133503602 
wait 0 -> 1133503602
1 -> 1133503603 *
wait 1 -> 1133503603
2 -> 1133503604 **
wait 2 -> 1133503604
3 -> 1133503605 ***
wait 3 -> 1133503605
4 -> 1133503606 ****
wait 4 -> 1133503606
5 -> 1133503607 *****
wait 5 -> 1133503607
6 -> 1133503608 ******
wait 6 -> 1133503608
7 -> 1133503609 *******
wait 7 -> 1133503609
8 -> 1133503610 ********
wait 8 -> 1133503610
9 -> 1133503611 *********
wait 9 -> 1133503611
End
[qiao@oicq qiao]$
从 多进程的例子可以看出,使用pcntl_fork()之后,将生成一个子进程,而且子进程运行的代码,从pcntl_fork()之后的代码开始,而子进 程不继承父进程的数据信息(实际上是把父进程的数据做了一个全新的拷贝),因而使用if(!$pids[$i]) 来控制子进程实际运行的代码段。
更详细的研究出于时间关系,暂时没有进行,你可以参考我给出的手册的链接。

[文章二] 尝试php命令行脚本多进程并发执行
除了fork, cli下的并发方式还有一种,看我的例子:
php不支持多线程,但是我们可以把问题转换成“多进程”来解决。由于php中的pcntl_fork只有unix平台才可以使用,所以本文尝试使用popen来替代。
下面是一个例子:
被并行调用的子程序代码:

复制代码 代码如下:

if($argc==1){
    echo("argvn");
}
$arg = $argv[1];
for($i=0; $i<10; $i++)
{
    echo($i.".1.".time()." exec $arg n");
    if($arg=='php2'){
        sleep(1);
        echo($i.".2.".time()." exec $arg n");
        sleep(1);
    }else{
        sleep(1);
    }
}
?>

主调用者程序,由他调用子进程,同时并发的收集子程序的输出
复制代码 代码如下:

error_reporting(E_ALL);
$handle1 = popen('php sub.php php1', 'r');
$handle2 = popen('php sub.php php2', 'r');
$handle3 = popen('php sub.php php3', 'r');
echo "'$handle1'; " . gettype($handle1) . "n";
echo "'$handle2'; " . gettype($handle2) . "n";
echo "'$handle3'; " . gettype($handle3) . "n";
//sleep(20);
while(!feof($handle1) || !feof($handle2) || !feof($handle3) )
{
$read = fgets($handle1);
echo $read;
$read = fgets($handle2);
echo $read;
$read = fgets($handle3);
echo $read;
}
pclose($handle1);
pclose($handle2);
pclose($handle3);

下面是我机器上的输出:
C:my_hunter>php exec.php
'Resource id #4'; resource
'Resource id #5'; resource
'Resource id #6'; resource
0.1.1147935331 exec php1
0.1.1147935331 exec php2
0.1.1147935331 exec php3
1.1.1147935332 exec php1
0.2.1147935332 exec php2
1.1.1147935332 exec php3
2.1.1147935333 exec php1
1.1.1147935333 exec php2
2.1.1147935333 exec php3
3.1.1147935334 exec php1
1.2.1147935334 exec php2
3.1.1147935334 exec php3
4.1.1147935335 exec php1
2.1.1147935335 exec php2
4.1.1147935335 exec php3
5.1.1147935336 exec php1
2.2.1147935336 exec php2
5.1.1147935336 exec php3
6.1.1147935337 exec php1
3.1.1147935337 exec php2
6.1.1147935337 exec php3
7.1.1147935338 exec php1
3.2.1147935338 exec php2
7.1.1147935338 exec php3
8.1.1147935339 exec php1
4.1.1147935339 exec php2
8.1.1147935339 exec php3
9.1.1147935340 exec php1
4.2.1147935340 exec php2
9.1.1147935340 exec php3
5.1.1147935341 exec php2
5.2.1147935342 exec php2
6.1.1147935343 exec php2
6.2.1147935344 exec php2
7.1.1147935345 exec php2
7.2.1147935346 exec php2
8.1.1147935347 exec php2
8.2.1147935348 exec php2
9.1.1147935349 exec php2
9.2.1147935350 exec php2
**总结:**
**主程序循环等待子进程, 通过fgets或fread 把子进程的输出获取出来 , 从时间戳上看,的确实现了并发执行。**
-----------------------------------------------
以后的改进:
*  popen打开的句柄是单向的,如果需要向子进程交互,可以使用proc_open
*  使用数组和子函数代替while(!feof($handle1)|| !feof($handle2) || !feof($handle3) )这种龌龊的写法
*  用fread一次把子进程已经产生的输出取完,而不是每次一行。
一个并发执行shell任务的调度者,本程序读取一个任务文件,把里面的每行命令并发执行, 可以设置同时存在的子进程数目:
复制代码 代码如下:

/*
Main task manager
Concurrent execution sub-task list
*/
include("../common/conf.php");
include ("../common/function.php");
//Number of opened processes
$exec_number = 40;
/***** main ********/
if($argc== 1){
echo("argvn");
}
$taskfile = $argv[1];
//tasklist
$tasklist = file($taskfile);
$tasklist_len = count($tasklist);
$tasklist_pos = 0;
$handle_list = array();
while(1)
{
//The child process list is free, then Fill in the child process list
if($exec_number > count($handle_list) &&
$tasklist_pos < $tasklist_len)
{
for($i=$tasklist_pos; $i<$ tasklist_len; )
                                                                 $command = $                                                                                                            tasklist[$i]);
$i++;
if($exec_number == count($handle_list)) break;
}
$tasklist_pos = $i;
}
//If the sub-process list is empty, exit
if(0 == count($handle_list))
{
break;
}
//Check the output of the sub-process list and stop The dropped child process is closed and recorded
$end_handle_keys = array();
foreach($handle_list as $key => $handle)
{
//$str = fgets($handle , 65536);
$str = fread($handle, 65536);
echo($str);
if(feof($handle))
$end_handle_keys[ ] = $key;
pclose($handle);
}
}
//Kick out the stopped child process
foreach($end_handle_keys as $key)
{
unset($handle_list[$key]);
            //var_dump($handle_list); ************end**********************nn", "" , true);



Attach a piece of code for Socket multi-process reception:




Copy the code

The code is as follows:
do {
if (($msgsock = socket_accept($sock)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "n"; break ; } $pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if (!$pid ) {
.....
socket_write($msgsock, $msg, strlen($msg));
do {
....
} while (true) ;
socket_close($msgsock);
}
} while (true);






http://www.bkjia.com/PHPjc/327960.html

www.bkjia.com
true

http: //www.bkjia.com/PHPjc/327960.html

(pseudo) multi-threading: Use external force to use the multi-threading of the WEB server itself for processing, and call multiple times from the WEB server We need to implement multi-threaded programs. QUOTE: We know that PHP itself is not supported...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!