当前位置:网站首页>Use of swarm task task
Use of swarm task task
2022-07-28 01:11:00 【Life goes on and battles go on】
Swoole asynchronous Task, It mainly realizes the execution of calling asynchronous tasks .
Common scenes : Asynchronous payment processing 、 Asynchronous order processing 、 Asynchronous log processing 、 Send mail asynchronously / SMS, etc. .
Swoole The implementation of worker The process handles data requests , Assigned to task Process execution .
The official introduction :
task Bottom use Unix Socket Pipeline communication , It's full memory , No, IO Consume . The read-write performance of a single process can reach 100 ten thousand /s, Different processes use different pipes to communicate , Can maximize the use of multi-core .
Local version :PHP 7.2.6、Swoole 4.3.1.
Not much to say , Look at the renderings first :
Code
server.php
class Server
{
private $serv;
public function __construct() {
$this->serv = new swoole_server('0.0.0.0', 9501);
$this->serv->set([
'worker_num' => 2, // Turn on 2 individual worker process
'max_request' => 4, // Every worker process max_request Set to 4 Time
'task_worker_num' => 4, // Turn on 4 individual task process
'dispatch_mode' => 2, // Packet distribution strategy - Fixed mode
]);
$this->serv->on('Start', [$this, 'onStart']);
$this->serv->on('Connect', [$this, 'onConnect']);
$this->serv->on("Receive", [$this, 'onReceive']);
$this->serv->on("Close", [$this, 'onClose']);
$this->serv->on("Task", [$this, 'onTask']);
$this->serv->on("Finish", [$this, 'onFinish']);
$this->serv->start();
}
public function onStart($serv) {
echo "#### onStart ####".PHP_EOL;
echo "SWOOLE ".SWOOLE_VERSION . " Service started ".PHP_EOL;
echo "master_pid: {$serv->master_pid}".PHP_EOL;
echo "manager_pid: {$serv->manager_pid}".PHP_EOL;
echo "########".PHP_EOL.PHP_EOL;
}
public function onConnect($serv, $fd) {
echo "#### onConnect ####".PHP_EOL;
echo " client :".$fd." Connected ".PHP_EOL;
echo "########".PHP_EOL.PHP_EOL;
}
public function onReceive($serv, $fd, $from_id, $data) {
echo "#### onReceive ####".PHP_EOL;
echo "worker_pid: {$serv->worker_pid}".PHP_EOL;
echo " client :{$fd} It's from Email:{$data}".PHP_EOL;
$param = [
'fd' => $fd,
'email' => $data
];
$rs = $serv->task(json_encode($param));
if ($rs === false) {
echo " Task assignment failed Task ".$rs.PHP_EOL;
} else {
echo " Task assigned successfully Task ".$rs.PHP_EOL;
}
echo "########".PHP_EOL.PHP_EOL;
}
public function onTask($serv, $task_id, $from_id, $data) {
echo "#### onTask ####".PHP_EOL;
echo "#{$serv->worker_id} onTask: [PID={$serv->worker_pid}]: task_id={$task_id}".PHP_EOL;
// Business code
for($i = 1 ; $i <= 5 ; $i ++ ) {
sleep(2);
echo "Task {$task_id} It's done {$i}/5 The task of ".PHP_EOL;
}
$data_arr = json_decode($data, true);
$serv->send($data_arr['fd'] , 'Email:'.$data_arr['email'].', Send successfully ');
$serv->finish($data);
echo "########".PHP_EOL.PHP_EOL;
}
public function onFinish($serv,$task_id, $data) {
echo "#### onFinish ####".PHP_EOL;
echo "Task {$task_id} Completed ".PHP_EOL;
echo "########".PHP_EOL.PHP_EOL;
}
public function onClose($serv, $fd) {
echo "Client Close.".PHP_EOL;
}
}
$server = new Server();client.php
<?php
class Client
{
private $client;
public function __construct() {
$this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$this->client->on('Connect', [$this, 'onConnect']);
$this->client->on('Receive', [$this, 'onReceive']);
$this->client->on('Close', [$this, 'onClose']);
$this->client->on('Error', [$this, 'onError']);
}
public function connect() {
if(!$fp = $this->client->connect("127.0.0.1", 9501 , 1)) {
echo "Error: {$fp->errMsg}[{$fp->errCode}]".PHP_EOL;
return;
}
}
public function onConnect($cli) {
fwrite(STDOUT, " Input Email:");
swoole_event_add(STDIN, function() {
fwrite(STDOUT, " Input Email:");
$msg = trim(fgets(STDIN));
$this->send($msg);
});
}
public function onReceive($cli, $data) {
echo PHP_EOL."Received: ".$data.PHP_EOL;
}
public function send($data) {
$this->client->send($data);
}
public function onClose($cli) {
echo "Client close connection".PHP_EOL;
}
public function onError() {
}
}
$client = new Client();
$client->connect();边栏推荐
- Rendering problems
- Retinanet网络结构详解
- Multithreading & high concurrency (the latest in the whole network: interview questions + map + Notes) the interviewer is calm
- Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
- Matlab 绘制 - 点和向量:向量加减的方法和源码
- Jerry caused other messages to accumulate in the message pool [article]
- I/O设备的基本概念及分类
- 福特SUV版“野马”正式下线,安全、舒适一个不落
- 共创文旅新篇章|新起典与国华文旅签订战略合作协议
- 学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
猜你喜欢

Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

C语言程序设计 | offsetof宏的讲解及其模拟实现

Syntaxerror resolved: positive argument follows keyword argument

一周年创作纪念日,冲吧少年郎

深度刨析数据在内存中的存储
![Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning

Use of postman

Swoole定时器
![Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]](/img/e3/ea7278eb855cca2a1af4f96772bcb1.png)
Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]
随机推荐
Scrollview, tableview nested solutions
芯片行业常用英文术语最详细总结(图文快速掌握)
The most detailed summary of common English terms in the chip industry (quick grasp of graphics and text)
Swoole内存-table详解
【原】【爬虫系列】简要获取一下知乎的最热门话题相关主题及描述信息
Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
一周年创作纪念日,冲吧少年郎
Redis-哨兵模式
小波变换学习笔记
At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning
R language uses ggplot2 visualization: use ggpattern package to add custom stripe patterns, shadows, stripes, or other patterns or textures to the grouped bar graph
One year anniversary of creation, Chongba young Lang
R language evaluates the relative importance of the predictive factors (variables, characteristics) of the regression model, scales the predictive variables of the regression model, and then construct
红队大杀器 Behinder_v4.0(冰蝎4.0)
线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量
推荐系统-模型:dssm双塔模型做embedding的召回
[proteus simulation] 51 single chip microcomputer washing machine simulation control program
Shell系统学习之循环结构
Jerry's prompt sound processing when switching devices [chapter]