Today’s PHPer, thanks to the rapid development of the front end and the popularity of the idea of separating the front and back ends, has freed many people from working on rendering templates.

With the prevalence of micro-services (servification), Swoole will become more and more powerful. PHP will not become more and more obsolete, but swoole will hold up another field of PHP.

As the PHP programming language is used by more and more people, more and more people are aware of Swoole’s existence. Some people say swoole is a C program in THE guise of PHP, but Swoole is not a simple PHP framework, just like the first sentence of the official Homepage of Swoole “redefines PHP”. Never write Swoole code with old PHP ideas!

Swoole’s development and perfection also contributed to the self-promotion of a large number of Phpers. Swoole reactivated PHP, and PHP made Swoole!

What is Swoole? Swoole is a “PHP asynchronous network communication engine for production environment”. Firstly, Swoole is a network application development tool, which supports Http, TCP, UDP and WebSocket. Currently, it is developed using some Framework based on Swoole. From the development custom and the traditional TP, LV framework is similar.

What does Swoole bring to PHP?

1. Asynchrony of class NodeJS:

There aren’t none, most notably Workerman and ReactPHP, both of which provide NodeJS class asynchrony and are implemented in PHP, which significantly reduces the chance of memory leaks and crashes compared to Swoole.

When asynchronous callbacks are faced with complex business logic, writing callbacks in nested and looping forms is not so intuitive. Not only do you have to save the context, but sometimes you have to write them recursively and jump around the callback chain. Swoole coroutines completely solve the asynchronous callback hell of complex business. There is no alternative to this.

2. Coroutine of class Go

Swoole’s coroutine client must be used in the context of the coroutine.

// The first case: $server->on('Request', function($Request, $response) {// create Mysql Coroutine client $Mysql = new Swoole\Coroutine\ Mysql (); $mysql->connect([]); $mysql->query(); }); // The second case: $server->on('WorkerStart', function() {$server->on('WorkerStart', function()); $Mysql = new Swoole\Coroutine\ Mysql (); $Mysql = new Swoole\Coroutine\ Mysql (); $mysql->connect([]); $mysql->query(); }); });Copy the code

Swoole’s coroutines are based on a single thread and cannot take advantage of multi-core cpus, with only one scheduled at a time.

$n = 4; $n = 4; for ($i = 0; $i < $n; {$i++) go (function () use ($I) {/ / simulation IO wait Co: : sleep (1); echo microtime(true) . ": hello $i " . PHP_EOL; }); }; echo "hello main \n"; $PHP test. PHP hello main 1558749158.0913: hello 0 1558749158.0915: hello 3 1558749158.0915: hello Hello 2 1558749158.0915: Hello 1Copy the code

Swoole coroutine use example and detailed explanation

$server = new Swoole\Http\ server ('127.0.0.1', 9501, SWOOLE_BASE); // When the onRequest event callback is called, the bottom layer will call the C function coro_create to create a coroutine, and save the CPU register state and the ZendVM stack information at this point in time. $server->on('Request', function($Request, $response) {$Mysql = new Swoole\Coroutine\ Mysql (); Coro_yield (coro_yield, coro_yield, coro_yield, coro_yield, coro_yield) The current request is suspended. // When the coroutine relinquishes control and continues to enter the EventLoop to process other events, Swoole will continue to process requests from other clients. $res = $mysql - > connect ([' host '= >' 127.0.0.1 ', 'user' = > 'root' and 'password' = > 'root', 'database' = > 'test']); // After the IO event completes, the MySQL connection succeeds or fails, and the bottom layer calls the C function coro_resume to restore the corresponding coroutine, restore the ZendVM context, and continue to execute the PHP code down. if ($res == false) { $response->end("MySQL connect fail"); return; $ret = $mysql->query('show tables', 2); $ret = $mysql->query('show tables', 2); // When all operations are complete, call the end method to return the result and destroy the coroutine. $response->end('swoole response is ok, result='.var_export($ret, true)); }); $server->start();Copy the code

3, built-in Redis/Http protocols

Swoole supports the most and most comprehensive protocols. Even if it does not support, it has a good chance to use hooks to support them automatically.

4, resident memory, avoid performance loss caused by repeated loading, improve massive performance

5. The existing blocking library is asynchronous/coprogrammed to improve the concurrent processing capability of IO intensive scenarios (such as wechat development, payment, login, etc.).

There is no alternative to asynchrony/coroutinization of existing blocking libraries. When Workerman and ReactPHP do use blocking libraries, they have to use multi-threaded or multi-process solutions.

Third, the skills that must be mastered in using Swoole

1. Multithreaded programming

2. Interprocess communication

3. Knowledge of network protocol TCP/UDP

4. Basic skills of PHP