当前位置:网站首页>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();边栏推荐
- oracle分组取最大值
- iperf安装与使用
- 学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
- 线性代数 【23】 概念的深入01 - Points坐标点和Vectors向量
- Basic use of calculation attributes
- When Jerry made a phone call, recording to SD card /u disk was not enough [article]
- Demo: the test interface receives duplicate data and creates documents in a short time
- 立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!
- Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte
- Learning note 12: Eratosthenes screening method to find prime numbers (within 100) and magic square matrix
猜你喜欢

Postman 的使用

"C language" deep entry rounding & four functions

函数相关知识

Focal Loss讲解

多线程及多线程程序的编写

Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism

Redis缓存穿透击穿和雪崩

Rancher2.6 monitoring grafana docking LDAP

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

C语言程序设计 | 单身狗题目讲解
随机推荐
推荐系统-模型:wide&deep 模型
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
[BuildRelease Management]Parabuild
Network equipment hard core technology insider firewall and security gateway (V) security double repair method
Oracle错误: ORA-01722 无效数字
I/O设备的基本概念及分类
oracle分组取最大值
Iperf installation and use
[CruiseControl]Build Result JSP
[CruiseControl]Build Result JSP
Examples of application of JMeter in performance testing
Use of postman
一周年创作纪念日,冲吧少年郎
Learning note 12: Eratosthenes screening method to find prime numbers (within 100) and magic square matrix
代码随想录笔记_哈希_1002查找共用字符
Circular structure of shell system learning
Jmeter在性能测试中的应用实践样例
论文赏析[ICLR18]联合句法和词汇学习的神经语言模型
DC motor winding parameters
Multithreading and multithreaded programming