PHP非阻塞模式
PHP非阻塞模式
by 尘缘 on 七月 31st, 2014 // Filed Under → php
让PHP不再阻塞当PHP作为后端处理需要完成一些长时间处理,为了快速响应页面请求,不作结果返回判断的情况下,可以有如下措施:
一、若你使用的是FastCGI模式,使用fastcgi_finish_request()能马上结束会话,但PHP线程继续在跑。
帮助
01 02 03 04 05 06 07 08 09 10 |
echo"program start.";
file_put_contents('log.txt','start-time:'.date('Y-m-d H:i:s'), FILE_APPEND); fastcgi_finish_request(); sleep(1); echo'debug...'; file_put_contents('log.txt','start-proceed:'.date('Y-m-d H:i:s'), FILE_APPEND);
sleep(10); file_put_contents('log.txt','end-time:'.date('Y-m-d H:i:s'), FILE_APPEND); |
这个例子输出结果可看到输出program start.后会话就返回了,所以debug那个输出浏览器是接收不到的,而log.txt文件能完整接收到三个完成时间。
二、使用fsockopen、cUrl的非阻塞模式请求另外的网址
帮助
1 2 3 4 5 6 7 8 |
$fp=fsockopen("www.example.com", 80,$errno,$errstr, 30); if(!$fp)die('error fsockopen'); stream_set_blocking($fp,0); $http="GET /save.php / HTTP/1.1\r\n"; $http.="Host: www.example.com\r\n"; $http.="Connection: Close\r\n\r\n"; fwrite($fp,$http); fclose($fp); |
利用cURL中的curl_multi_*函数发送异步请求
帮助
1 2 3 4 5 6 |
$cmh= curl_multi_init(); $ch1= curl_init(); curl_setopt($ch1, CURLOPT_URL,"http://localhost:6666/child.php"); curl_multi_add_handle($cmh,$ch1); curl_multi_exec($cmh,$active); echo"End\n"; |
三、使用Gearman、Swoole扩展
Gearman是一个具有php扩展的分布式异步处理框架,能处理大批量异步任务;
Swoole最近很火,有很多异步方法,使用简单。(尘缘注:号称重新定义PHP,把NodeJS喷得体无完肤。Swoole工具虽好,却感觉是扩展本身跟NodeJS没可比性)
四、使用redis等缓存、队列,将数据写入缓存,使用后台计划任务实现数据异步处理。
这个方法在常见的大流量架构中应该很常见吧
五、极端的情况下,可以调用系统命令,可以将数据传给后台任务执行,个人感觉不是很高效。
帮助
1 2 |
$cmd='nohup php ./processd.php $someVar >/dev/null &'; `$cmd` |
六、外国佬的大招,没看懂,php原生支持
http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html
七、安装pcntl扩展,使用pcntl_fork生成子进程异步执行任务,个人觉得是最方便的,但也容易出现zombie process。
帮助
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 |
if(($pid= pcntl_fork()) == 0) { child_func(); //子进程函数,主进程运行 }else{ father_func(); //主进程函数 }
echo"Process ".getmypid() ." get to the end.\n";
functionfather_func() { echo"Father pid is ".getmypid() ."\n"; }
functionchild_func() { sleep(6); echo"Child process exit pid is ".getmypid() ."\n"; exit(0); } |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
