Implementing multi-threading in PHP? When you see this title, you must think I am crazy... But in fact, I really do it.
Here are some of my methods, which have been tested. It is indeed possible.
We know that PHP itself is not It supports multi-threading, but our WEB server supports multi-threading.
In other words, it can be accessed by multiple people at the same time. This is also the basis for me to implement multi-threading in PHP.
Assume that we are running the file a.php now. . But I also request the WEB server to run another b.php in the program
,so the two files will be executed at the same time.
(PS: After a link request is sent, the WEB server will execute it, regardless of whether the client has exited. )
Sometimes, what we want to run is not another file, but a part of the code in this file. What should we do?
In fact, we can use parameters to control which program a.php runs.
Here is an example:
//a .php
PHP code:------------------------------------------------ ----------------------------------
<?php
function runThread()
{
$fp = fsockopen('localhost', 80, $errno, $errmsg);
fputs($fp, "GET /a.php?act=brnrn"); //The second parameter here is the request header specified in the HTTP protocol
//If you don’t understand, please see the definition in RFC
fclose($fp);
}
function a()
{
$fp = fopen('result_a.log', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn");
fclose($fp);
}
function b()
{
$fp = fopen('result_b.log', 'w');
fputs($fp, 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn");
fclose($fp);
}
if(!isset($_GET['act'])) $_GET['act'] = 'a';
if($_GET['act'] == 'a')
{
runThread();
a();
}
else if($_GET['act'] == 'b') b();
?>
------------------------------------------------ --------------------------------
Open result_a.log and result_b.log and compare the access times of the two files. You will find that these two are indeed running in different threads.
Some times are exactly the same.
The above is just a simple example, and you can improve it into other forms.
Now that multi-threading is available in PHP, a problem arises, that is, synchronization. We know that PHP itself does not support multi-threading. So there is no method like
synchronize in Java. So what should we do? What to do.
1. Try not to access the same resource to avoid conflicts. But you can operate the database at the same time. Because the database supports concurrent operations,do not write data to the same file
in multi-threaded PHP
. If necessaryIf you want to write, use other methods for synchronization. For example, call flock to lock the file, etc. Or create a temporary file
and wait for the disappearance of the file in another thread while(file_exits('xxx')); This is equivalent to When this temporary file exists, it means that the thread is actually operating
. If there is no such file, it means that other threads have released it.
2. Try not to read data from this socket after runThread executes fputs. Because to achieve multi-threading, it is necessary The non-blocking mode is used. That is, it returns immediately when a function
like
fgets is used. So there will be problems when reading and writing data. If the blocking mode is used, the program is not multi-threaded. It has to wait for the above return before executing.The following program. So if you need to exchange data, you can finally use external files or data to complete it. If you really want it, use socket_set_nonblock($fp) to achieve it.
Having said so much, does this have any practical significance? When is it necessary to use this method?
The answer is yes. As we all know, in an application that continuously reads network resources, the speed of the network is Bottleneck. If you adopt this method, you can
read different pages with multiple threads at the same time.
I made a program that can search for information from mall websites such as 8848 and soaso. There is also a program that reads business information and company directories from the Alibaba website and also uses
this technology. Because both programs have to continuously connect to their servers to read information and save it to the database. Utilizing this technology eliminates the bottleneck of waiting for a response.