非阻塞模式是指利用socket事件的消息机制,Server端与Client端之间的通信处于异步状态。
让PHP不再阻塞当PHP作为后端处理需要完成一些长时间处理,为了快速响应页面请求,不作结果返回判断的情况下,可以有如下措施:
一、若你使用的是FastCGI模式,使用fastcgi_finish_request()能马上结束会话,但PHP线程继续在跑。
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的非阻塞模式请求另外的网址
$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_*函数发送异步请求
$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等缓存、队列,将数据写入缓存,使用后台计划任务实现数据异步处理。
这个方法在常见的大流量架构中应该很常见吧
五、极端的情况下,可以调用系统命令,可以将数据传给后台任务执行,个人感觉不是很高效。
$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。
if (($pid = pcntl_fork()) == 0) { child_func(); //子进程函数,主进程运行 } else { father_func(); //主进程函数 } echo "Process " . getmypid() . " get to the end.\n"; function father_func() { echo "Father pid is " . getmypid() . "\n"; } function child_func() { sleep(6); echo "Child process exit pid is " . getmypid() . "\n"; exit(0); }
以上内容是小编给大家介绍的PHP非阻塞模式,希望对大家有所帮助!

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
