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

What are the differences between blocking and non-blocking in php programs

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "what is the difference between blocking and non-blocking in php programs". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is the difference between blocking and non-blocking in php programs?"

The difference between PHP program blocking and non-blocking is that the current thread is suspended and the caller does not continue to execute until the result of the blocking call returns, while the call does not block the current thread and can continue to execute until the result of the non-blocking call returns.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

What is the difference between blocking and non-blocking in php programs

Blocking and non-blocking are concerned with the state of the program while waiting for the result of the call (message, return value).

A blocking call means that the current thread is suspended before the result of the call is returned. The calling thread does not return until it gets the result.

A non-blocking call means that the call does not block the current thread until the result is not immediately available.

1.php concurrent blocking

Concurrency is a problem for php. We often encounter some queries before writing the database to determine whether the data exists, but if it is concurrent, it will lead to repeated data writing, and your judgment has become false. So with blocking, we need to execute the request one by one.

A brief introduction:

1. First, open or create a file lock.txt file in read-write mode

two。 Put an "exclusive lock" on the lock.txt file, and after the lock is successful, you can proceed to the next step of "processing order item data".

3. After processing the data, release the lock, and fclose closes the open file

Note: after giving the file an "exclusive lock", if there is no "release lock" inside, it will be very stuck.

Public function index () {$fp = fopen ("lock.txt", "w +"); if (flock ($fp,LOCK_EX)) {$find=Db::name ('user')-> where (' username','name2')-> find (); if ($find) {$data ['username'] =' name3'; $data ['password'] ='; $data ['password_m'] ='' Db::name ('user')-> insert ($data);} else {$data [' username'] = 'name2'; $data [' password'] =''; $data ['password_m'] =''; Db::name ('user')-> insert ($data);} flock ($fp,LOCK_UN);} fclose ($fp) Return 'success';}

Note: it is feasible for small concurrency, and there is no big impact on performance. Concurrency is less than 500 is better, if too high, it is recommended to use queue mode.

2.php non-blocking mode

In non-blocking mode, we often call third-party api interfaces within php programs, or programs that do not need to wait for results.

Give me a simple example. Send an email, you want to send it to all your bosses, then you need to send it all, maybe you have to write a loop, then the method of sending email in this loop requires execution time, needs to get the return value, and executes the next loop. this time accumulates in turn, and the final time will be very long.

So this is called blocking, and what we've always wanted is to submit the past, you don't have to wait for the data to be returned, you just run it in a loop, so what we're thinking about is non-blocking mode. Php doesn't have any good functions to deal with this kind of words, so what we do most is when we get out of the queue and send e-mails.

To execute according to the queue, the blocking mode is actually used. But our request execution time will be compressed very short, the most common is to call the API API, and you do not care about the return value at this time.

At this point, I believe you have a deeper understanding of the difference between blocking and non-blocking in php programs, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report