Analyze the multi-thread in PHP ~

xiaoxiao2021-03-06  78

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, have been experimentally. It is indeed.

We know that PHP itself does not support multi-threaded, but our web server is supporting multi-threaded.

That is to say, many people can be accessed together. This is also the basis for my multithreading in PHP.

Suppose we are now running a.php file. But I asked the web server in the program to run another B.php

Then these 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, through parameters to control A.php to run which program.

Let's take a look at one example:

//a.php

PHP code: ----------------------------------------------- ---------------------------------

----- -------------------------------------------------- -------------------------

Open Result_a.log and Result_b.log Compare the time of the two files. You 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 it into other forms.

Since PHP can also be multi-threaded, then the problem has come, that is, the problem of synchronization. We know that PHP itself does not support multi-threaded. So don't even have anything like

The method of Synchronize in Java. What should we do?

1. Try not to access the same resource. To avoid conflicts. But you can operate like a database. Because the database is supported by the concurrency operation. So in the multi-threaded PHP

Do not write data to the same file. If you have to write, use other methods to synchronize .. If you call Flock to lock the file, you can lock the file. (file_exits ('xxx')); this is equal to this temporary file, indicating that it is active

If there is no file, other threads have been released.

2. Try not to read data from RuntHread to take this socket after performing FPUTS. Because multiple threads are required, non-blocking mode is required. Ie, like fgets

When the function is changed. Therefore, it will be problematic if you read and write data. If you use the blocking mode, the program is not a multi-thread. He wants to wait for the above return

The following program. So if you need to switch the data, you will be done in the outside file or data. If you want, use socket_set_nonblock ($ fp) to be implemented.

Said so much, is there no practical significance? When do you need this way to use this method?

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 simultaneously use multiple threads.

Different pages are read.

I made a program from 8848, SOASO's website search information. There is also a program that reads business information and company catalogs from the Alibaba website.

This technology is. Because both programs are to link their servers to read information and save them to the database. Using this technology just eliminates the bottle waiting for a response

neck.

Just throwing bricks, I hope everyone will discuss ~~~~~~~

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

New Post(0)