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");
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
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
TechArticle(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...