Table of Contents
被swoole坑哭的PHP程序员,swoole坑程序员
遇到的几点问题:" >遇到的几点问题:
1:关于phper常用的全局变量(global)为什么在onRequest函数中不能使用。" >1:关于phper常用的全局变量(global)为什么在onRequest函数中不能使用。
2:什么是异步、什么是回高" >2:什么是异步、什么是回高
3:为什么onReceive收到的数据这么大" >3:为什么onReceive收到的数据这么大
4:自制httpserve" >4:自制httpserve
原因" >原因
使用swoole必须要掌握的技能" >使用swoole必须要掌握的技能
个人学习swoole的经历" >个人学习swoole的经历
总结" >总结
Home php教程 php手册 被swoole坑哭的PHP程序员,swoole坑程序员

被swoole坑哭的PHP程序员,swoole坑程序员

Jun 13, 2016 am 08:54 AM
php swoole one time main pit study of programmer Record

被swoole坑哭的PHP程序员,swoole坑程序员

    本文主要记录一下学习swoole的过程、填过的坑以及swoole究竟有多么强大!

    首先说一下对swoole的理解:披着PHP外衣的C程序。很多PHPer朋友看到swoole提供的强大功能、外界对其的崇拜便跃跃欲试的安装、调试其demo、编写新功能,然后兴奋的奔走相告。没过几天当你按照自己的理解继续用swoole时,发现代码并没有按照自己的预期运行,然后开始破口大骂,什么破东西呀,代码跟demo基本一样,为啥运行不通呢?什么狗屁work、task、共享内存、ipcs、异步,各种问题涌现,然后迅速去查官方文档,发现文档中竟然对这些并没有提及,只是简单的介绍怎么使用,此时几乎对swoole丧失希望。

遇到的几点问题:

1:关于phper常用的全局变量(global)为什么在onRequest函数中不能使用。

因为swoole是多线程编程,global是不能在多个进程间共享的。例

global $i = 0;

function onRequest() {

    echo $i++;

}

如果在swoole中写一个上面的程序,并不会每次访问输出一个递增的数字。如果要实现预期的效果,需要使用swoole_table的相关函数。

2:什么是异步、什么是回高

对于phper来说,对异步、回调的理解估计就是ajax。当看到swoole里面对异步、回调的解释,貌似很简单的样子,就这样在没有任何多线程编辑经验的时候贸然用了swoole,结果被坑的偷偷撸代码好几个通宵来填自己的坑。

3:为什么onReceive收到的数据这么大

客户端发送的多次请求,服务端是可以一次性接收的。并不是客户端发送一次,服务端接收一次

4:自制httpserve

写一个http服务端,然后通过浏览器访问这个自制的服务器,刷新一次浏览器,服务端为什么为接收到两次请求?这个问题估计困饶了好多初次用swoole写httpserver的朋友。因为浏览器会多发一个favicon.ico请求。

原因

出现这种情况的原因其实很简单,大部分phper都只会php这一种语言,主要用途就是做web,写业务逻辑。很少去了解服务器程序的开发。有一次一个朋友用swoole写了一个简单的服务端,一个客户端,跑过来问我为什么都启动了却都收不到数据,我简单看了下代码,所有连接确实都成功了,两端都设置了onReceive回调,代码没问题,看到最后才发现他的服务端、客户端都设置了接到消息的回调函数,但是两端都没有向对方发消息,两端处于僵持状态。然后swoole官方对于这种常识问题没有给出说明,只是说如何设置回调、如何发消息,如何这样,如何那样。对于有服务端开发经验的同学来说,肯定不会遇到这种问题,swoole文档也不需要指明需要这样做,因为这是常识。但对于phper来说,指明这一点是非常重要的,因为如上面所说phper是没有这方面认知的,只有服务端开发经验的程序员有才会有。

swoole的特色:网络通信框架、异步、多线程。这些特性正是php所不完善的功能(虽然官方提供很多基础函数可以实现这些功能,然后缺少中文文档,很少有人用php来实现这部分功能),普通的phper也不具备这些特性的基础认知,所以贸然使用swoole难免会遇到一些根本在swoole官方查不到的常识问题。

使用swoole必须要掌握的技能

  1. 多线程编程
  2. 进程间通信
  3. 网络协议TCP/UDP的认知
  4. PHP的各项基本技能

个人学习swoole的经历

在很久之前我也是一个只会php的程序员,后来一次偶然机会需要用httpsqs,用了一段时间后发现有一些个性的需求,于是就开始看源码。这真是不看不知道,一看吓一跳,httpsqs只是一层简单的包装,内部是一个Tokyo Cabinet数据库,印象中封装的代码也就一百多行。主要思路就是用C语言的libevent做了一个http服务器,接收请求读写tokyo cabinet数据库,当时按照这种思路做出来的程序确实不少。后来我就突发奇想,既然C语言可以用libevent函数,那PHP肯定也可以用libevent监听网络,接收请求后读写数据库做队列服务。后来经过查php官方文档,PHP确实提供一系统完整的函数来完成这些功能,甚至多线程的全套函数都有提供,但中文文档太少,网上也很少搜索到成熟的代码。在逼不得已的情况下,补习了linux-C多线程开发的基本原理,进程间通信的常用方法,也用来做了一些简单的demo。唯一的感觉就是写一个简单的功能,设计起来还真复杂。就在快要放弃的时候,swoole出现了。swoole所提供的功能正是php所缺失的功能,简直是太棒了。swoole做为一种网络通信框架,只需要简单的几行设置,一个服务器就搭建起来了,以后就是不断的去完善业务代码。之前在libevent交流群中得知swoole的设计在c\c++中并不是最好的框架设计,但其亮点就是把基本功能用C封装好,业务功能留给世界上最好的语言PHP来编写。自此便开始了swoole的填坑之旅。

总结

swoole并不是一个简单的PHP框架,正如swoole官方首页的第一句话“重新定义PHP”,千万不要用旧有php的思想来写swoole代码!swoole重新激活了PHP,php成就了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
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 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
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.

See all articles