PHP programmer swoole Pit Cry, swoole pit programmer _php Tutorial

Source: Internet
Author: User
Tags php framework what php

PHP programmer, Swoole pit programmer, swoole pit cry


This paper mainly records the process of learning swoole, filled the pits and swoole how powerful!

First of all, the understanding of Swoole: A C program draped in a PHP cloak. Many phper friends see Swoole provide the powerful features, the outside of its worship will be eager to install, debug its demo, write new features, and then excited excitedly spread. Not a few days when you follow your own understanding to continue to use swoole, found that the code did not run according to their own expectations, and then began to curse, what broke things ah, the code with the demo basically the same, why not run it? What kind of work, task, shared memory, IPCS, asynchronous, a variety of problems emerge, and then quickly go to the official documents, found that the document did not mention these, but simply introduce how to use, at this time almost lost hope for Swoole.

Some of the problems encountered:

1: About Phper the commonly used global variables (global) are not available in the ONrequest function.

Because Swoole is multithreaded programming, global cannot be shared among multiple processes. Cases

Global $i = 0;

function ONrequest () {

echo $i + +;

}

If you write an above program in Swoole, it does not output an incremented number each time you access it. If you want to achieve the desired effect, you need to use the related functions of swoole_table.

2: What is async, what is back high

For Phper, an understanding of async and callbacks is an estimate of Ajax. When see swoole inside of the asynchronous, callback explanation, seemingly very simple appearance, so in the absence of any multi-threaded editing experience rushed to use the swoole, the result was the secret of the pit code several night to fill their pits.

3: Why OnReceive received so much data

The client sends multiple requests and the server can be received at one time. is not sent once by the client and received once by the server

4: Homemade Httpserve

Write an HTTP server, and then access the home-made servers through a browser, refresh the browser, the service side why to receive two requests? The problem is estimated to be a lot of friends who first wrote Httpserver with Swoole. Because the browser will send more than one favicon.ico request.

Reason

The reason for this is actually very simple, most phper are only php this language, the main purpose is to do the Web, write business logic. Little to know about the development of server programs. Once a friend with Swoole wrote a simple server, a client, ran to ask me why all start up and not receive data, I simply looked at the code, all the connections did succeed, both ends are set OnReceive callback, the code is not a problem, see finally found his server, The client sets a callback function to the message, but neither side sends a message to the other, and the ends are deadlocked. Then swoole the official for this common sense question does not give the explanation, just says how to set up the callback, how to send the message, how so, how so. For those of you who have experience with server-side development, this is certainly not a problem, and the Swoole documentation does not need to be specified because it is common sense. But for Phper, it is important to point this out because, as mentioned above, Phper is not aware of this, only programmers with server-side development experience will have it.

Swoole Features: Network communication framework, asynchronous, multi-threaded. These features are PHP's imperfect features (although the official offer many basic functions can implement these functions, and then the lack of Chinese documents, very few people use PHP to achieve this part of the function), ordinary phper do not have the basic knowledge of these features, So rushed to use swoole will inevitably encounter some fundamental in the Swoole official can not find common sense problem.

Skills that you must master to use Swoole

    1. Multithreaded programming
    2. Inter-process communication
    3. Cognition of Network Protocol TCP/UDP
    4. Basic PHP Skills

The experience of individual learning Swoole

A long time ago, I was also a PHP programmer, then a chance to use HTTPSQS, after a while to find some personality needs, so began to look at the source code. This is really not to see, a look scare, HTTPSQS is just a layer of simple packaging, the inside is a Tokyo cabinet database, the impression of the encapsulated code is more than 100 lines. The main idea is to use the C language Libevent made an HTTP server, receive requests to read and write to the Tokyo Cabinet database, at that time in accordance with this idea of the program did a lot. Then I had a whim, since C language can use Libevent function, that PHP must also be able to use Libevent Listening network, receive the request read and write database to do queue service. Later, after checking the official PHP document, PHP does provide a complete system of functions to complete these functions, even multi-threaded full set of functions are provided, but the Chinese documents are too few, the Internet rarely search for mature code. In the case of forced, remedial linux-c the basic principle of multithreading development, the common method of inter-process communication, also used to do some simple demo. The only feeling is to write a simple function, the design is really complicated. As soon as I was about to give up, Swoole appeared. The functionality that Swoole provides is just what PHP lacks, and it's fantastic. Swoole as a network communication framework, only need a few simple lines of setup, a server is set up, the future is constantly to improve the business code. Before in the Libevent Communication group learned that Swoole design in c\c++ is not the best frame design, but its highlight is the basic ability to use C package, business functions left the world's best language PHP to write. Since then, Swoole's pits tour has begun.

Summarize

Swoole is not a simple PHP framework, just as the first sentence of the official homepage of swoole "redefine PHP", do not use the old php idea to write swoole code! Swoole reactivated the php,php achievement swoole!

http://www.bkjia.com/PHPjc/1055138.html www.bkjia.com true http://www.bkjia.com/PHPjc/1055138.html techarticle a PHP programmer who was swoole, Swoole pit Programmer This paper mainly records the process of learning swoole, filled pits and swoole how powerful! First of all , the understanding of 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.