PHP asynchronous execution method simulation of multithreaded application analysis

  • 2020-06-03 06:02:23
  • OfStack

PHP does not have multi-threading itself, but can be curated to achieve the same effect, such as a multi-process approach to asynchronous calls, restricted to command mode.
An even simpler way to use Web programs is to request an URL using fsockopen() and fputs() without waiting for the return, which is asynchronous if you do something in that requested page (URL).
The key codes are as follows:

<?php
  $fp = fsockopen('localhost',80,&$errno,&$errstr,5);  
  if(!$fp)
     {
         echo "$errstr ($errno)<br />/n"; 
     }
     fputs($fp,"GET another_page.php?flag=1/r/n"); 
     fclose($fp);

The above code sends the request to the page another_page.php, and instead of waiting for the response data from the requested page, you can do something asynchronously in the requested page another_page.php.
For example, in a very practical application, every time we publish a new log, we need to send an email notification to all subscribers of the log. If we follow the usual way, it is as follows:
Log finished - > Click the submit button - > Log is inserted into the database - > Send mail notification - > Inform the writer that the release was successful
Then the author may wait a very long time between hitting the submit button and seeing a successful notification, basically waiting for the mail to be sent, such as connecting to the mail service is abnormal, or the server is slow or there are too many subscribers. In fact, it is basically acceptable to ensure that the log is saved successfully regardless of whether the mail is sent or not. Therefore, the process of waiting for the mail to be sent is very uneconomical. This process can be executed asynchronously, and the results of the mail are not cared about or recorded in the form of log for future reference.
The improved process is as follows:
Log finished - > Click the submit button - > Log is inserted into the database -- > Inform the writer that the release was successful
Send an email └ - > [Keep a journal]
Write an actual program to test 1. There are two files, write.php and sendmail.php. sendmail.php USES sleep(seconds) to simulate the execution time of the program.
write.php, execution time: 1 second:

 <?php  
  function asyn_sendmail() 
     { 
         $fp = fsockopen('localhost',80,&$errno,&$errstr,5);  
      if(!$fp)
         {
            echo "$errstr ($errno)<br />/n";
         } 
     sleep(1); 
     fputs($fp,"GET /sendmail.php?param=1/r/n"); # Requested resources  URL 1 Need to write on  
     fclose($fp); 
    }  
 echo time().'<br>'; 
 echo 'call asyn_sendmail<br>'; 
 asyn_sendmail(); 
 echo time().'<br>'; 

sendmail.php, execution time: 10 seconds:

<?php 
sleep(10); 
fopen("C:/" . time(),  "w");  

write. php, page output:
1272472697
call asyn_sendmail
1272472698
And in C:/ generate file:
1272472708
The above results show that ES64en.php takes at least 10 seconds but does not block ES66en.php to continue, indicating that this 1 process is asynchronous.

Related articles: