Table of Contents
简单介绍PHP非阻塞模式,简单介绍php阻塞
Home php教程 php手册 简单介绍PHP非阻塞模式,简单介绍php阻塞

简单介绍PHP非阻塞模式,简单介绍php阻塞

Jun 13, 2016 am 08:45 AM
php udp

简单介绍PHP非阻塞模式,简单介绍php阻塞

非阻塞模式是指利用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);
Copy after login

这个例子输出结果可看到输出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);
Copy after login

利用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";
Copy after login

三、使用Gearman、Swoole扩展

Gearman是一个具有php扩展的分布式异步处理框架,能处理大批量异步任务;
Swoole最近很火,有很多异步方法,使用简单。(尘缘注:号称重新定义PHP,把NodeJS喷得体无完肤。Swoole工具虽好,却感觉是扩展本身跟NodeJS没可比性)

四、使用redis等缓存、队列,将数据写入缓存,使用后台计划任务实现数据异步处理。

这个方法在常见的大流量架构中应该很常见吧

五、极端的情况下,可以调用系统命令,可以将数据传给后台任务执行,个人感觉不是很高效。

$cmd = 'nohup php ./processd.php $someVar >/dev/null &';
`$cmd`
Copy after login

六、外国佬的大招,没看懂,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);
}
Copy after login

以上内容是小编给大家介绍的PHP非阻塞模式,希望对大家有所帮助!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles