php是如何实现多线程并发的

不言
发布: 2018-07-20 16:55:35
原创
15716人浏览过

php 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 --enable-maintainer-zts 参数重新编译 php,这个参数是指定编译 php 时使用线程安全方式。

<?php 
if(function_exists('date_default_timezone_set')) { 
date_default_timezone_set('PRC'); 
} 
function a() { 
$time = time(); sleep(3); 
$fp = fopen('result_a'.$time.'.log', 'w'); 
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); 
fclose($fp); 
} 
function b() { 
$time = time(); 
sleep(3); 
$fp = fopen('result_b'.$time.'.log', 'w'); 
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"); 
fclose($fp); 
} 
if(!isset($_GET['act'])) $_GET['act'] = 'a'; 
if($_GET['act'] == 'a') { 
a(); 
} 
else if($_GET['act'] == 'b') b(); 
?>
登录后复制

以上代码,在本地写入一个文件。

PHP多线程读写文件:

如果你访问 localhost/a.php 在两个浏览器标签尽可能快的同时打开,发现两个文件创建时间相差为3秒

但是如果你访问localhost/a.php?act=b 另一个访问/a.php?act=a 你发现两个文件创建的时间几乎差不多。

立即学习PHP免费学习笔记(深入)”;

对于apache来说同样的url意味着一个线程(我们或者说是进程),但是不同的URL意味着可以 并发 。

如果php内部有下载的动作

function runThread() { 
down("http://localhost/test/a.php?act=a"); 
} 
if($_GET['act'] == 'run') { 
echo 'start:'; 
runThread(); 
echo ' End'; 
}
登录后复制

http://localhost/test/a.php?act=run

http://localhost/test/a.php?act=run&s=2

只要主访问的url不同,则认为是不同的进行,意味着并发。文件创建时间不为3秒

本地有Linux服务器的朋友也可以借助linux来进行模拟并发

<?php for ($i=0;$i<10;$i++) { echo $i; sleep(5); } ?>
登录后复制

上面存成test.php, 然后写一段SHELL代码

#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
php -q test.php &
done
Fixed a bug where :doc:`Image Manipulation Library <libraries/image_lib>` didn't escape image source paths passed to ImageMagick as shell arguments.
Fixed a bug (#861) - :doc:`Database Forge <database/forge>` method create_table() incorrectly accepts field width constraints for mssql/SQLSRV integer-type columns.
Fixed a bug (#4562) - :doc:`Cache Library <libraries/caching>` didn't check if Memcached::quit() is available before calling it.
Fixed a bug (#4563) - :doc:`Input Library <libraries/input>` method request_headers() ignores $xss_clean parameter value after first call.
Fixed a bug (#4605) - :doc:`Config Library <libraries/config>` method site_url() stripped trailing slashes from relative URIs passed to it.
Fixed a bug (#4613) - :doc:`Email Library <libraries/config>` failed to send multiple emails via SMTP due to "already authenticated" errors when keep-alive is enabled.
Fixed a bug (#4633) - :doc:`Form Validation Library <libraries/form_validation>` ignored multiple "callback" rules for empty, non-required fields.
Fixed a bug (#4637) - :doc:`Database <database/index>` method error() returned FALSE with the 'oci8' driver if there was no error.
Fixed a bug (#4647) - :doc:`Query Builder <database/query_builder>` method count_all_results() doesn't take into account GROUP BY clauses while deciding whether to do a subquery or not.
Fixed a bug where
登录后复制

相关推荐:

php 实现多线程,php多线程

以上就是php是如何实现多线程并发的的详细内容,更多请关注php中文网其它相关文章!

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号