search
HomeBackend DevelopmentPHP Tutorialswoole-1.8.0 发布,PHP 的异步并行 C 扩展

Swoole-1.8.0 版本已发布,此版本是一个里程碑式新版本,新增了多项新特性、多项核心功能优化以及问题修复、移除了无效的特性。更新内容如下:

客户端

  • 增加原生异步 MySQL 客户端

  • 增加原生异步 Redis 客户端,基于 Redis 官方提供的 hiredis 库

  • 增加原生异步 Http 客户端

  • 增加原生异步 WebSocket 客户端支持

  • 重构底层 swClient,异步 TCP 客户端实现放到 swoole 内核中

  • 增加 swoole_client->reuse 属性,SWOOLE_KEEP 长连接模式下标识是否为复用的连接

服务器端

  • 重构 websocket 服务器代码,底层与 length_check 协议复用相同的处理函数,增强稳定性

  • 增加 Task 进程对 tick/after 定时器的支持,底层基于高精度的 setitimer+ 信号实现

  • 保存构造函数中传入的 host、port 参数到 swoole_server 对象属性

  • 增加多端口多协议的支持(重要更新)

  • 增加 swoole_server->defer 函数用于延时执行一些函数

  • 增加 swoole_server->close 强制切断连接的选项,设置第二个参数会 true 会清空发送队列并立即切断连接

多端口多协议示例:

$serv = new swoole_server("0.0.0.0", 9501);$port2 = $serv->listen('127.0.0.1', 9502, SWOOLE_SOCK_TCP);$port2->set(array(    'open_length_check' => true,    'package_length_type' => 'N',    'package_length_offset' => 0,       //第N个字节是包长度的值    'package_body_offset' => 4,       //第几个字节开始计算长度    'package_max_length' => 2000000,  //协议最大长度));$port2->on('receive',  function (swoole_server $serv, $fd, $from_id, $data)  {    echo "ServerPort2\n";});$serv->on('connect', function ($serv, $fd, $from_id){    echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Connect.\n";});$serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) {    echo "[#".$serv->worker_id."]\tClient[$fd]: $data\n";    if ($serv->send($fd, "hello\n") == false)    {        echo "error\n";    }});$serv->on('close', function ($serv, $fd, $from_id) {    echo "[#".posix_getpid()."]\tClient@[$fd:$from_id]: Close.\n";});$serv->start();

其他

  • 增加swoole_table对key值的存储,foreach遍历table时可以获取到key值

  • 更改swoole_table的key对比模式,从crc32比对改为直接进行字符串对比

  • 更新utlist.h库到1.9.9版本

swoole_table保存Key值会增加内存占用,如table的size为100万,KEY值存储会增加64M内存占用

问题修复

  • 修复启用消息队列后发生double-free问题

  • 重构定时器,修复after、tick定时器偶然出现的core dump的问题

  • 定时器使用最小堆数据结构,插入/删除时间复杂度为log(N)

  • 修复swoole_process::signal在PHP7下发生core dump的问题

  • 修复swoole_async_write在PHP7下发生core dump的问题

移除特性

  • 移除未支持的特性相关历史遗留代码,如heartbeat_ping、dispatch_key_type等

  • 移除swoole_server->addtimer、swoole_server->deltimer、swoole_server->gettimer

  • 移除swoole_timer_add、swoole_timer_del

  • 移除swoole_server的onTimer事件

  • 移除task_worker_max配置及相关特性代码

  • 移除swoole_server->handler方法

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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

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

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.