前言
public function demo50() {
$num = 10;
$filename = 'process.txt';
if ($fp = fopen($filename, 'a')) {
for ($i = 0; $i<= $num; $i++) {
fwrite($fp, 'process1:' . $i . "\r\n");
usleep(100000);
}
fclose($fp);
} else {
echo 'faile';
}
}
public function demo51() {
$num = 10;
$filename = 'process.txt';
if ($fp = fopen($filename, 'a')) {
for ($i = 0; $i<= $num; $i++) {
fwrite($fp, 'process2:' . $i . "\r\n");
usleep(100000);
}
fclose($fp);
} else {
echo 'faile';
}
}
public function demo53() {
// 创建一对cURL资源
$ch1 = curl_init();
$ch2 = curl_init(); // 设置URL和相应的选项
curl_setopt($ch1, CURLOPT_URL, "http://local.thinkphp.com/index.php?h=home&c=index&a=demo50");
curl_setopt($ch2, CURLOPT_URL, "http://local.thinkphp.com/index.php?h=home&c=index&a=demo51"); // 创建批处理cURL句柄
$mh = curl_multi_init(); // 增加2个句柄
curl_multi_add_handle($mh, $ch1);
curl_multi_add_handle($mh, $ch2); $running = null;
// 执行批处理句柄
do {
curl_multi_exec($mh, $running);
} while ($running > 0); // 关闭全部句柄
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
}
运行结果如下
加上文件锁后
public function demo50() {
$num = 10;
$filename = 'process.txt';
if ($fp = fopen($filename, 'a')) {
if (flock($fp, LOCK_EX)) {
for ($i = 0; $i<= $num; $i++) {
fwrite($fp, 'process1:' . $i . "\r\n");
usleep(100000);
}
flock($fp, LOCK_UN);
}
fclose($fp);
} else {
echo 'faile';
}
}
public function demo51() {
$num = 10;
$filename = 'process.txt';
if ($fp = fopen($filename, 'a')) {
if (flock($fp, LOCK_EX)) {
for ($i = 0; $i<= $num; $i++) {
fwrite($fp, 'process2:' . $i . "\r\n");
usleep(100000);
}
flock($fp, LOCK_UN);
}
fclose($fp);
} else {
echo 'faile';
}
}
PHP文件锁:
摘自:http://www.cnblogs.com/ninelands/archive/2012/09/18/2690713.html