In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "PHP synchronous and asynchronous difference and fsockopen asynchronous operation", in daily operation, I believe many people in PHP synchronous and asynchronous difference and fsockopen asynchronous operation problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer "PHP synchronous and asynchronous difference and fsockopen asynchronous operation" doubts helpful! Next, please follow the small series to learn together!
Difference between synchronous and asynchronous:
1, synchronous mode: after a task waiting for the end of the previous task, and then execute, the execution order of the program and the order of the task is consistent, synchronous.
Asynchronous mode: it is completely different, each task has one or more callback functions (callback), after the end of the previous task, not to execute the next task, but to execute the callback function, the latter task is not to wait for the end of the previous task to be executed, so the execution order of the program and the order of the tasks are inconsistent and asynchronous. Asynchronous mode is very important. On the browser side, time-consuming operations should be performed asynchronously to avoid the browser losing response. For now, there should be no background server or synchronous operation.
For example, when users grab tickets, they do not want users to queue up to connect to the database for query, judgment, insertion, and return user results after completion. In fact, we do not need users to wait for so long time, after the user submits, directly tell him that he has successfully snatched tickets can, as for various operations, to the background to deal with good. Of course, we now use message lists to handle this situation. We store each request submitted by the user in a message queue, telling the user that it has been done. After the user happily closes the page, in fact, the background is still taking requests from the message queue one by one. Our article is through a heterogeneous approach to achieve operations in the background, without the user waiting.
Second, fsockopen asynchronous
For php, the biggest disadvantage is that it cannot achieve multithreaded management. The execution of its programs is from beginning to end. It is impossible to branch according to logic.
1. Creating a time-consuming method that requires operation
//Cycle through database 1w times public function test(){ $i=input('id')? input('id'):1; while ( $i $i. '-'.date('Y-m-d H:i:s'), 'num' => $i]; Db::name('input')->insert($data); $i++; } echo 'complete'; }
2, we created a function based on fsockopen, this function uses fsockopen to access the url, but in the access, does not require access to the url display content, but only issued an access request, the request arrived immediately after the close of the access. The advantage of doing this is that you don't have to wait for the URL to return reliable information, saving time. The execution time of this code is between 0.1 and 0.2 seconds, which is almost imperceptible to ordinary visitors. Therefore, when used, you only need to call this function and the corresponding url. However, there is no part of the data transmission provided here, how to transmit data, in fact, only need to add the content of the post in the $header.
/** * @2018/12/24 19:25:06 * [asynchronous PHP executes tasks asynchronously] * @param string $url Url address of task execution * @param array $post_data Required post Submitted data POST * @param array $cookie cookie data is used for settings such as login (internal call here, no authentication required) * @return boole */public function asynchronous($url,$post_data = array()) { $url_array = parse_url($url); dump($url_array); //try to connect with fsockopen() $fp = fsockopen($url_array['host'], 80, $errno, $errstr, 30); if (!$ fp) { echo "$errstr ($errno)\n"; } else { //write data to server after successful establishment $getPath = isset($url_array['path']) ? $url_array['path'] : '/'; $out = "GET /".$ getPath. "/ HTTP/1.1\r\n"; $out .= "Host:".$ url_array['host']. "\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); /* Ignore execution results while (! feof($fp)) { echo fgets($fp, 128); }*/ //Close link fclose($fp); } }
3. Create a tuning method
//call function yibu(){ /** * asynchronous method * Parameters:(string) url of the method to be executed,(array) passing parameters */ $this->asynchronous('http://119.23.33.37/index.php/admin/index/test',['1'=>'haha']); //Return results directly echo 'operation successful';}
4, browser access to the method (directly return to the operation results, a large number of loop background slowly executed)
III. Swiole asynchronous operation
Create an asynchronous server
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.