Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Asynchronous non-blocking in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to achieve asynchronous non-blocking in PHP, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

FastCGI non-blocking method: fastcgi_finish_request()

After PHP version 5.3.3, whether it is Nginx or Apache server, as long as it runs in FastCGI mode, it can be used. The official explanation is to flush all the response data to the client.

boolean fastcgi_finish_request ( void )

This function flushes all the response data to the client and ends the request. This allows tasks that take a lot of time to run to continue running after the client ends the connection.

Usage: You can execute this function before reading and writing large files, updating the database cyclically, etc. without affecting the result, and return the result to the client. PHP will continue to execute the following logic without affecting the client's response time.

fsockopen()+stream_set_blocking() Method:

The fsockopen() method can open a network connection or an Unxi socket connection, the stream_set_blocking() method can set a non-blocking or blocking mode for a resource stream,

Use fsockopen() to open a network connection or a Unix socket connection, and then use stream_set_blocking() to set the resource to non-blocking mode requests, so the resource request will be non-blocking:

bool stream_set_blocking ( resource $stream , int $mode )

Note: $mode=0 is non-blocking and 1 is blocking.

Usage:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report