Home  >  Article  >  Backend Development  >  Is php single threaded or multi-threaded?

Is php single threaded or multi-threaded?

(*-*)浩
(*-*)浩Original
2019-09-27 09:30:054703browse

From the beginning of PHP's design to its popularity, there has been no obvious need to use multi-threading to solve it.

Is php single threaded or multi-threaded?

#There are also corresponding solutions and alternatives where multi-threading is required. (Recommended learning: PHP programming from entry to proficiency)

Multiple threads are not always better than single threads. Multi-threads may introduce other problems (for example: two threads calling one at the same time) When using the same method in a class, a deadlock may occur).

You can understand that the PHP corresponding to a page request of a customer is processed in a single thread, so that you can edit/understand the business logic in the code from top to bottom. However, PHP can open many threads at the same time to handle the same PHP requested by many users, so PHP can also be regarded as "multi-threaded".

The execution of each PHP file is single-threaded, however, the server (apache/nigix/php-fpm) is multi-threaded. Every time a certain PHP file is accessed by the server, a new process/thread will be created to execute the corresponding PHP file.

That is to say PHP is single-threaded for one request, but multiple requests are concurrent.

In fact, generally when writing PHP programs, it is considered to be single-threaded; the relationship between multiple requests is that sometimes reading and writing databases, files, sessions, etc. Locking will cause subsequent requests to hang until the previous request is completed before continuing.

As for the coroutine, it can only be said to be a new program execution process (the old one is sequence, judgment, loop), and its essence is also single-threaded

So to be precise, PHP is single-threaded, and to a certain extent it can also be regarded as "multi-threaded"! ! !

The above is the detailed content of Is php single threaded or multi-threaded?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Whether php enables gdNext article:Whether php enables gd