PHP programmers who are crying by swoole. The PHP programmer crying by swoole this article mainly records the process of learning swoole, the traps filled in, and how powerful swoole is! First, let's talk about swoole's understanding: a PHP programmer who is crying by swoole in a PHP coat
This article mainly records the process of swoole learning, the traps filled in, and how powerful swoole is!
First, let's take a look at swoole's understanding: C programs dressed in PHP. Many PHPer friends are eager to install, debug their demo, and write new functions when they see the powerful functions provided by swoole. A few days later, when you continue to use swoole according to your own understanding, you will find that the code is not running as expected, and then you will start to scold me. what are the things? the code is basically the same as the demo, why can't I run it? Various problems such as work, task, shared memory, ipcs, and asynchronization emerged. then, I quickly checked the official documents and found that the documents did not mention these issues, this is just a brief introduction to how to use it. at this time, almost no hope for swoole.
Several problems encountered:
1: about the global variables commonly used by phper) why is it not available in the onRequest function.
Because swoole is multi-thread programming, global cannot be shared among multiple processes. Example
- global $i = 0;
-
- function onRequest() {
-
- echo $i++;
-
- }
If you write a program in swoole, it does not output an increasing number every time you access it. To achieve the expected results, use the swoole_table function.
2: What is asynchronous and what is high?
For phper, the understanding of asynchronization and callback is estimated to be ajax. When we see the asynchronous and callback interpretations in swoole, it seems very simple, so we use swoole without any multi-thread editing experience, as a result, the code was secretly written for several nights to fill in the trap.
3: Why is the data received by onReceive so large?
The server can receive multiple requests from the client at a time. Not once sent by the client, but once received by the server
4: homemade httpserve
Write an http server, access the self-made server through a browser, and refresh the browser once. Why does the server receive two requests? This problem is estimated to be difficult for many friends who used swoole to write httpserver for the first time. Because the browser will send more favicon. ico requests.
Cause
The reason for this situation is actually very simple. most phper only uses the php language. The main purpose is to do web and write business logic. I seldom know about the development of server programs. Once a friend used swoole to write a simple server, a client, and ran to ask why I started but couldn't receive any data. I simply read the code, all connections are successful, and the onReceive callback is set at both ends. the code is correct. only then can the server and client have set the callback function for receiving messages, however, neither end sends a message to the other end, and both ends are deadlocked. Swoole does not provide a description of this common problem, but it only describes how to set the callback, how to send messages, and how to do so. For those who have experience in server development, this problem is certainly not encountered, and the swoole document does not need to specify the need to do so, because this is common sense. But for phper, it is very important to specify this point, because as mentioned above, phper does not have such knowledge. only programmers with server development experience can have it.
Swoole features: network communication framework, asynchronization, and multithreading. These features are just the incomplete functions of php. although many basic functions are officially provided to implement these functions, there is a lack of Chinese documents. few people use php to implement these functions ), ordinary phper does not have the basic cognition of these features, so it is inevitable that swoole will encounter common sense problems that are not found at the official swoole.
Skills required to use swoole
-
Multi-thread programming
-
Inter-process communication
-
Cognition of TCP/UDP protocol
-
Basic PHP skills
Personal swoole learning experience
A long time ago, I was also a php programmer. later I had an occasional opportunity to use httpsqs. after a while, I found some personalized requirements, so I started to look at the source code. I really don't know. I was shocked. httpsqs is just a simple package. inside it is a Tokyo Cabinet database, and there are more than one hundred lines of encapsulated code in the impression. The main idea is to use libevent in C language as an http server to receive requests to read and write the tokyo cabinet database. at that time, there were indeed a lot of programs based on this idea. Later, I had a whim. since the C language can use the libevent function, PHP can certainly use the libevent to listen to the network and read and write the database for queue service after receiving the request. After checking the official php documents, PHP does provide a complete system function to complete these functions, and even a full set of multi-threaded functions are provided, but there are too few Chinese documents, mature code is rarely found on the Internet. The basic principles of linux-C multi-thread development and common methods of inter-process communication were used for some simple demos. The only feeling is to write a simple function, which is really complicated to design. Swoole appeared as soon as it was about to give up. The functions provided by swoole are just the functions missing by php, which is amazing. Swoole is a network communication framework. it only requires a few simple lines to set up a server. in the future, it will constantly improve the business code generation. I learned from the libevent discussion group that swoole's design is not the best framework design in c \ c ++, but its highlight is that the basic skills can be encapsulated in C, business functions are reserved for PHP, the world's best language. Since then, swoole has started its journey of filling holes.
Summary
Swoole is not a simple PHP Framework. just as swoole's official homepage says "redefining PHP", do not use the old php idea to write swoole code! Swoole re-activates PHP and php achieves swoole!
This article describes how to learn swoole, how powerful swoole is, and how powerful it is! First of all, let's take a look at swoole's understanding...