Analyze the multi-thread in PHP ~

zhaozj2021-02-12  161

Analyze the multi-thread in PHP ~

Multi-thread in PHP? See this title, you must think that I am crazy .. But in fact I really do this. Below is some of my practices, it is really. We know that PHP itself is not Support multithreaded, but our web server is supported by multi-threaded. That is to say, many people can be accessed together. This is also the basis for the multi-thread in PHP. Suppose we are running now is a.php this file. But I asked the web server in the program to run another B.php. Then 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, we want to run another file, but a part of this document. What should I do? In fact, by parameters to control a.php to run which program. Look at an example: // a .php

PHP code:

?>

Open Result_a.log and Result_b.log Compare the time of the two files. Everyone will find that these two are indeed running in different threads. Some time is exactly the same. It is just a simple example. You can improve Since the PHP can also be multi-thread, then the problem is coming, that is, the problem of synchronization. We know that PHP itself does not support multi-threaded. So don't have any way to synchronize in Java. Then we should do it. 1. Try not to access the same resource. To avoid conflicts. But you can operate simultaneously. Because the database is supported by concurrency operation. So don't write in the same file in the multi-threaded PHP. Data. If you must write, use other methods to synchronize .. If you call FLOCK to lock the file or so, or build a temporary file and wait for this file in another thread to disappear while (file_exits ('xxx')) When this is equal to this temporary file, it means that the active thread is operating. If there is no file, other threads have been released .2. Try not to read data from Runthread after executing FPUTS. Because Multi-threaded, needed to use non-blocking mode. That is, immediately returned when a function like fgets. Therefore, read and write data will have problems. If you use the blocking mode, the program is not a multi-thread. He wants to wait above Returns the following program. So if you need to exchange data, you will be done in the outside file or data. If you want, use socket_set_nonblock ($ fp) to implement. So much, do you have any practical significance? When do you need this kind of way? The answer is yes. Everyone knows. In an application that constantly reading network resources, the speed of the network is bottleneck. If you have a form of this form, you can use multiple threads at the same time. Reading different pages. I made a program that can search information from 8848, SOASO website website. There is also a program that reads business information and company catalogs from Alibaba websites. Because both programs are to link their servers to read information and save them to the database. This technology uses this technology just eliminates the bottleneck waiting in response. Just throwing bricks, I hope everyone will discuss ~~~~~~~ _________________ him answered, if there is no special indication, never tested .... ============ ===================== "Japanese people also?" I asked God. On the face of God, it is ashamed, and the support is half-day, "count, calculate, just." I patted his shoulders, "This is not your true heart." God rose red face, turned and shed. Looking at him, I can't bear it, but I think it is not sweet to catch him. "If you lose your hair, you can make a batch of animals, but you can't put them next to the Chinese!" God Cry the face, "What is the Chinese people who die, what is the benevolence, and go to the past early, nothing to do so early.

转载请注明原文地址:https://www.9cbs.com/read-7412.html

New Post(0)