Php does not support multithreading, so you don't need to consider the concurrency issue? This sentence, right?

Source: Internet
Author: User
If you have any reason, thank you. If you have any reason, thank you.

Reply content:

If you have any reason, thank you.

In general, concurrency is mostly for services, such as apache nginx.
Instead of php
In addition, php has multi-thread extension, but it is not used in projects at ordinary times.

Define multi-threaded extends Thread

In fact, this sentence is ambiguous.

First, check the prerequisites:

Php does not support multithreading

PHP code itself (in most cases) does not care whether it is multi-process or multi-thread. However, this does not mean that PHP does not support multithreading/multi-process. Php-fpm is multi-process and single-thread, and apeche's multi-thread mode is multi-thread. PHP generally does not directly control processes or threads at the PHP code level.

No need to consider concurrency issues

If the premise is not true, the conclusion will have no causal relationship.
I understand the intention of this sentence: because PHP generally does not support control processes and threads, it will not directly control processes and threads through code to deal with concurrency issues.
So there is no problem.

However, the concurrency problem is still a concurrency problem. it does not exist because PHP code itself does not support solving the concurrency problem.
The general way to solve PHP concurrency problems is through a variety of configuration adjustment (nginx. conf, php-fpm.ini, php. ini), and then load balancing and other methods to solve. These are not PHP code, but they are closely related to PHP and need to be mastered as PHPer.

In addition, PHP logic is modified for specific business types, and even the front-end call logic is also available. There are also extensions such as swoole that completely abandon php-fpm and support asynchronous concurrency (but still single thread) based on PHP ). These concurrency optimization methods should be selected for specific services.

Wrong!

PHP does not support multithreading, but the background running of the command line program or the php-fpm of the web application can be processed concurrently by multiple processes, so the concurrency problem cannot be avoided, for example, if an order has one inventory and two concurrent requests come up at the same time, how do you ensure that the order is not sold too much?

Even if the multi-thread concurrency still exists, and it is more difficult to solve the problem, we still need to find a way to avoid and optimize it.

Is concurrency only multi-thread? This is too narrow.

PHP originally supports multithreading, so there is a difference between thread security and non-thread security.
For details about the multithreading extension of PHP, refer:
Https://pecl.php.net/package/pthreads
This extension provides real PHP multi-threaded programming support, which is generally used for script programming under cli.
In addition, Fengge also developed an extended Swoole that provides asynchronous multi-thread architecture support for developing high-performance real-time network services with PHP:
Https://pecl.php.net/package/swoole
The multithreading in Swoole does not need to be concerned by programmers. it is more like a set of architecture. you only need to configure it. Swoole is generally used for script programming under cli.

For example, the PHP FastCGI service PHP-FPM often used with Nginx is to use multi-process implementation to cope with concurrency using multi-core, similar to Apache using prefork MPM. PHP-FPM supports process pool settings, supports static and dynamic process count settings, supports natural transparent "database connection pool" (persistent connection ):

MOD_PHP works with Apache running with event MPM, because Apache event MPM is a multi-process and multi-thread event-driven MPM, which is a thread-safe version for PHP.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.