Why use Swoole
swooleThe strength is in the design of its process model, which solves both the asynchronous problem and the parallelism.
Swoole complete collaboration (Coroutine) + channel (channel) feature is available from version 4.0 to bring a full
CSP programming model. The application layer can use a fully synchronized programming approach, with asynchronous IO automatically implemented at the bottom.
Using resident memory mode avoids the initialization of each frame and saves performance overhead. The 4.x bottom layer uses automated synergy conversion,
What is a co-process
Coroutine is historically longer than the history of threads, and the synergy can be understood as a purely user-like thread that switches through collaboration rather than preemption. All the operations of the coprocessor can be done in the user state, and the consumption of creation and switching is lower than that of the process or thread. Swoole can create a corresponding synergy for each request, according to the state of IO to reasonable scheduling coordination.
Developers can achieve the effects and performance of asynchronous IO in a synchronous code writing way without perception, avoiding the discrete code logic and falling into multi-layered callbacks caused by traditional asynchronous callbacks that prevent code from being maintained
At the same time, because the underlying package of the interface, so compared to the traditional PHP layer collaboration framework, developers do not need to use
yield keywords to identify a coprocessor operation, so no longer need to have a
yield deep understanding of the semantics and every level of the call is modified to
yield , which greatly improves the development efficiency
Here is a comparison:
|Mode of communication||IPC inter-process pass||Data Synchronization/Lock||Array/chan|
|Resource consumption||Process switching Overhead||Process switching Overhead||Very low|
|Programming difficulty||Difficult||Very difficult||Easy|
Advantages of the co-process
- User-State threads, encountered IO active giveaway
- PHP code is still executed serially without the need for locks
- Extremely low overhead, only memory consumption, no process/thread switching overhead
- Large amount of concurrency, a single process can open 50W collaboration
- Anytime, anywhere, as long as you want to concurrency, call go to create a new collaboration
Swoole's collaboration and Golang scheduling are completely different, and the coordination within each process is performed so there is
串行 no need to worry about accessing resource locking issues, which also conforms to the simple nature of PHP.
So the synergy of the process is
串行how to implement the multi-core implementation
并行. The answer is to use multi-process implementations. Now task can also turn on the synergy.
This may not be as good as Golang performance, but for it
IO 密集型业务 is very suitable, the context of the co-process switch very quickly. The cost of switching a language is also huge for the company, and now Swoole's ecology is getting better.
4.0 the underlying join
Hook mechanism uses native
Mysql PDO , the
Redis operation will be directly co-structured, and subsequent extensions will be supported
Curl . Closer to traditional business code, migration costs are also reduced.
http server use of excellent
Reactor 模型 , processing speed can be forced into
NGINX the processing of static pages of speed. For
基础服务 very suitable. Performance instantly flip a few times, and do not have to worry about the number of php-fpm processes too many, resulting in
CPU being filled.
Where swoole needs attention.
Of course, it's not without its faults.
Unable to do intensive calculations. Of course, this is a problem with PHP and even all dynamic languages. Written here because it prevents misleading readers from thinking
php they can be used to do intensive calculations.
Easier memory leaks. Be careful when working with global variables, static variables, a variable that will not be cleaned by the GC will exist throughout its lifecycle, and if it is not handled correctly, it is easy to consume all the memory. In the past php-fpm, the PHP code will be completely released after executing the memory.
CSP has a classic phrase: Do not communicate by sharing memory, but should share memory through communication
This "communication" you can understand the use of
Although the Swoole is serial, the business may intersect , for example, you have a business in the add configuration of a business in the write configuration, the context of all changes, may cause configuration inconsistencies, you may be curious that the local run is okay to run on the line for a period of time will go wrong, This is a matter of business design.