PHP + Swoole concurrent programming charm

PHP language is a short life cycle of Web programming languages, many PHPer has formed a programming mindset under fpm. In fact, after Swoole appear, this serial programming model has already been broken. Use Swoole can easily achieve a more flexible concurrent programming.

Application Scenario
Suppose we want to be a rock-paper-scissors of Web games, displaying the winner after three players to submit quiz. In traditional serialized Web programming, we have the general idea is this:

Set form form, saving the user submits the quiz to MySQL / Redis store
to add a button to see the results, if not completed, the display is waiting for someone else to submit. When all three individuals to submit, query storage, and display the final results of
concurrent programming
this scene on concurrent programming Swoole implementation can use, without relying on MySQL / Redis storage can be completed quiz in memory.

When a user submits a quiz, hold live request does not return a result, the user enters a wait state. And the current connection request held in memory
when the content of all personal Submit 3, taken from memory related requests, calculates and sends a response to all requests traverse
Coding

 1 <?php
 2 $server = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_BASE);
 3 $result = [];
 4 $server->on('request', function ($req, $resp) use(&$result) {
 5 $resp->header('Content-Type', 'text/html; charset=UTF-8');
 6 if ($req->server['request_method'] == 'GET') {
 7 $resp->end('
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 9 <form method="post" action="">
10 <input type="radio" value="石头" name="result">石头
11 <input type="radio" value="剪刀" name="result">剪刀
12 <input type="radio" value="布" name="result">布
13 <button type="submit">提交</button>
14 </form>
15 ');
16 } else {
17 $result[$req->get['name']] = [$req, $resp];
18 if (count( $result) == 3) {
19 $out = '';
20 foreach($result as $arr) {
21 [$_req, $_resp] = $arr;
22 $out .= $_req->get['name'] ." : ". $_req->post['result']."<br />\n";
23 }
24 foreach($result as $arr) {
25 [$_req, $_resp] = $arr;
26 $_resp->end($out);
27 }
28 $result = [];
29 }
30 }
31 });
32 $server->start();

 

execute program

php game.php

 

Open 3 Chrome Tab page. Afferent name and URL are A, B, C representing three users.

 

 

 

 

 

 

 

In the first, the second time of submission of the results, did not return any results, Chrome is circling waiting for the server to return results. When the form is submitted three third Tab page and returns the result.

 

 

Concurrency problems
concurrent programming is more powerful than serial programming and more complex. Concurrent programming will encounter new problems before serial programming does not have, such as:

Data synchronization
context management of
timing issues
that require developers to have more rigorous engineering thinking skills, but also those who have a need to develop a more profound programming skills.

Paradigm shift
Swoole actually overturn the PHP programming mode, making the programmer's perspective is no longer limited to the processing of a request, no longer limited to databases CURD operations, interface calls. Used in conjunction with coroutine Swoole4 provides programmability, can be achieved in a variety of complex interactions memory space.

The new programming model that allows PHPer easy to achieve online games, server systems, smart home, networking and other projects.

Guess you like

Origin www.cnblogs.com/a609251438/p/11836701.html