Home >Backend Development >PHP Tutorial >php实现多线程_PHP

php实现多线程_PHP

WBOY
WBOYOriginal
2016-06-01 12:32:36855browse

PHP本身是不支持多线程的, 但apache是支持多线程的
我知道的有2种:
1,同页面iframe,这个比较不好,不能很好实现参数传递
2,例子

go.php

[CODE][function runThread()
{
$fp = fsockopen('localhost', 80, $errno, $errmsg);

fputs($fp, "GET /go.php?act=b\r\n\r\n");

fclose($fp);
}

function a()
{
$fp = fopen('1.txt', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "\r\n");

fclose($fp);
}

function b()
{
$fp = fopen('2.txt', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "\r\n");

fclose($fp);
}

if(!isset($_GET['act'])) $_GET['act'] = 'a';

if($_GET['act'] == 'a')
{
runThread();
a();
}
else if($_GET['act'] == 'b') b();
?>/CODE]

速度够快的话你可以发现 1.txt 和 2.txt 有些时间一样的

Statement:
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