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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

The Key to Coding: Unlocking the Power of Python for Beginners The Key to Coding: Unlocking the Power of Python for Beginners Oct 11, 2024 pm 12:17 PM

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Problem-Solving with Python: Unlock Powerful Solutions as a Beginner Coder Oct 11, 2024 pm 08:58 PM

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid

Demystifying C: A Clear and Simple Path for New Programmers Demystifying C: A Clear and Simple Path for New Programmers Oct 11, 2024 pm 10:47 PM

C is an ideal choice for beginners to learn system programming. It contains the following components: header files, functions and main functions. A simple C program that can print "HelloWorld" needs a header file containing the standard input/output function declaration and uses the printf function in the main function to print. C programs can be compiled and run by using the GCC compiler. After you master the basics, you can move on to topics such as data types, functions, arrays, and file handling to become a proficient C programmer.

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

See all articles