(pseudo) multi-threading: with the help of external force
Utilize 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 now. But I request the WEB server to run another b.php in the program
Then these two files will be executed at the same time.
(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 we do?
In fact, you can control which program a.php runs through parameters.
Look at an example below:
[php] view plaincopyThe 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 write data to the same file. If you must write, use other methods for synchronization. For example, call flock to lock the file, etc. 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 this file no longer exists, 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, like fgets
Such a function returns immediately. 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 above return before executing
The following program. So if you need to exchange data, you can finally use external files or data to complete it. 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 form, you can use multiple threads at the same time
Read different pages.
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
This technology. Because both programs have to continuously connect to their servers to read information and save it to the database. Utilizing this technique just eliminates the bottle while waiting for a response
Neck.
Multi-process: Use PHP’s Process Control Functions (PCNTL/thread control function)
Function reference can be found at: http://www.php.net/manual/zh/ref.pcntl.php
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 run it only in CLI mode, not in a WEB server environment.
The following is a short test code:
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命令行脚本多进程并发执行
作者:dulao5
来源:http://dulao5.blog.hexun.com/3726837_d.html
除了fork, cli下的并发方式还有一种,看我的例子:
php不支持多线程,但是我们可以把问题转换成“多进程”来解决。由于php中的pcntl_fork只有unix平台才可以使用,所以本文尝试使用popen来替代。
下面是一个例子:
被并行调用的子程序代码:
[php] view plaincopy
主调用者程序,由他调用子进程,同时并发的收集子程序的输出
[php] view plaincopy
下面是我机器上的输出:
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任务的调度者,本程序读取一个任务文件,把里面的每行命令并发执行, 可以设置同时存在的子进程数目:
[php] view plaincopy
原文地址:http://www.alixixi.com/program/a/2008050731686.shtml