Home php教程 php手册 Node.js的颠覆者:PHP的Swoole扩展

Node.js的颠覆者:PHP的Swoole扩展

Jun 10, 2016 pm 03:08 PM
node.js php swoole code Open source Expand programming programming language software development

      最近2年Node.js很火,异步与协程是网络开发方面热门的话题。在追求新技术的同时,也应该反思下这里面存在的陷阱。Node.js确实是一门有趣好玩有个性的语言和技术,动态性,全异步回调的方式,闭包等等特性。值得所有程序员尝试一下。
 
     但本文将介绍的是一门另外的容易被大家轻视的编程语言:PHP。长期以来PHP都是作为一门为Web开发而生的前端脚本语言。PHP极其的简单,你可以在一周只能掌握它,一月内开发出一个功能丰富的网站。发展至今PHP其实已经完全不再是一门简单的语言了,PHP的功能保罗万象,常用的操作系统功能,如进程管理,信号,网络通信,多线程,ptrace、inotify、加密解密、压缩都有相应的扩展实现,而且PHP可以很好的与C/C++互相调用。PHP提供了ZendAPI,可以很方便地使用C来扩充PHP的功能。语言特性方面PHP5.4提供的namespace,phar打包,composer依赖管理,Trait,完整的面向对象编程语法,强大的魔术方法和常量,字符串与函数类对象直接转换,闭包和匿名函数等丰富的语言特性。在后端开发方面强大到堪比Java,C#,但开发效率更高。


PHP对比Node.js的优势:

1、PHP开发效率更高

PHP比Node.js更简单直接,这一点有点像C了。使用PHP开发一个功能,几乎是所有语言中效率最高的,没有之一。
 

2、PHP程序员更多

PHP因为比较容易入门的原因,程序员数量远超其他语言。其他语言程序员也有很大一部分会PHP。
 

3、PHP开源项目多

PHP有大量开源的项目,有各种第三方库
 
Node.js最大的特色之一是内置了异步高性能的Socket Server/Client实现,在此基础上提供了内置的Web服务器。PHP里也有类似的神器,那就是Swoole扩展。使用Swoole扩展完全可以开发出一个高性能安全稳定的服务器程序来。丝毫不逊于Node.js,而且在某些方面比Node.js更强大。
 
Swoole使用C语言编写,以PHP扩展的方式来运行。Swoole的网络IO部分基于epoll/kqueue事件循环,是全异步非阻塞的。业务逻辑部分使用多进程同步阻塞方式来运行。这样既保证了Server能够应对高并发和大量TCP连接。又保证业务代码仍然可以简单的编写。
 

Swoole对比Node.js的优势:

1、swoole是原生支持多进程/多线程的

开发者只需要修改一个参数,配置下要启动多少个进程即可。而Node.js的网络库本身并没有提供多进程/多线程的实现。开发者需要自行创建进程。或者干脆使用单线程。这样无法充分利用多核。


2、swoole使用消息传递+多Worker进程,而不是多线程+共享内存+加锁

,共享内存的性能虽然很好,但存在安全问题,需要读写时加锁。锁的粒度过大会导致只有一个线程在运行。锁太复杂又会有死锁问题。所以开发者需要非常谨慎小心。


3、swoole的代码编写是同步,而不是嵌套异步回调

Node.js的代码如果太复杂,会嵌套多层回调,使代码丧失可读性,程序流程变得很乱。Swoole使用了传统Linux下半同步半异步多Worker的实现方式。业务代码按照同步方式编写,更简单。
Swoole也内置了Socket客户端的实现,但采用的是同步+并行方式来执行。PHP本身也提供了socket的功能,但某几个函数存在一些bug,而且比较复杂。Swoole内置的客户端类更加安全和简化。


4、swoole内置了Node.js所没有的额外特性

如CPU Affinity设置,守护进程化,混合UDP/TCP多端口监听,多定时器等。

Swoole项目地址:

https://github.com/matyhtf/swoole
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

See all articles