Fputs ($fp, "get/a.php?act=b\r\n\r\n"); The second parameter here is the request header specified in the HTTP protocol
I don't understand. Please see the definition in the RFC
Fclose ($FP);
}
function A ()
{
$fp = fopen (' Result_a.log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "\ r \ n");
Fclose ($FP);
}
Function B ()
{
$fp = fopen (' Result_b.log ', ' W ');
Fputs ($fp, ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "\ r \ n");
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 to compare the time of access in two files. As you can see, these two are actually running on different threads.
Some time is exactly the same.
The above is just a simple example that can be improved into other forms.
Since PHP can also be more than a thread, then the problem has come, that is the problem of synchronization. We know that PHP itself does not support multithreading. So there won't be anything like
The Synchronize method in Java. So how do we do that?
1. Try not to access the same resources. To avoid conflicts. However, you can operate as a database at the same time. Because the database is a concurrency-enabled operation. So in the multithreaded PHP
Do not write data to the same file. If you have to write, use a different method to sync. such as calling flock to lock the file. or create temporary files
And wait for this file to disappear while in another thread (file_exits (' xxx ')); This is equal to the presence of this temporary file, which means that the thread is actually operating
If this file is not there, other threads have released this.
2. Try not to read the data from the runthread after the execution of the socket fputs. Because to implement multithreading, you need to use non-blocking mode. That's like fgets this
Returns immediately when the function is a. So reading and writing data can be problematic. If you use blocking mode, the program is not multi-threaded. He's going to wait for the above return to execute.
The following procedure. So if you need to exchange data, you end up using outside files or data. If you really want it, use Socket_set_nonblock ($FP) to achieve it.
Having said so much, does this have any practical significance? When does this need to be used in this way?
The answer is yes. You know. In a continuous reading of network resources, the speed of the network is the bottleneck. If you take more of this form, you can simultaneously use multiple threads to
Different pages to read.
I do a can from 8848, Soaso these mall website search information program. There's also a program that reads business information and company catalogs from Alibaba's website
The technology. Because both programs are constantly linking their servers to read the information and save it to the database. The use of this technique eliminates the bottleneck in waiting for a response.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.