PHP programmers and swoole programmers crying by swoole

Source: Internet
Author: User

PHP programmers and swoole programmers crying by swoole

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 its demo, and write new functions when they see the powerful functions provided by swoole. A few days later, when you continue using swoole according to your own understanding, you will find that the Code does not run as expected, and then you will start to swear. 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 went to the official documents and found that these issues were not mentioned in the documents, this is just a brief introduction to how to use it. At this time, almost no hope for swoole.

Several Problems encountered:1: Why is the common global variable (global) in phper 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 wrote a simple server and a client using swoole and ran to ask me 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 an official explanation of this common sense problem. 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 not perfect for php (although many basic functions are officially provided to implement these functions, there is a lack of Chinese documentation, and 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
  1. Multi-Thread Programming
  2. Inter-process communication
  3. Cognition of TCP/UDP protocol
  4. 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 my impressions. 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 of settings, and a server is built up. In the future, it will constantly improve the Business Code. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.