swoole —— 從新定義PHPphp
swoole 的進程之間有兩種通訊方式,一種是消息隊列(queue),另外一種是管道(pipe),對swoole_process 的研究在swoole中顯得尤其重要。html
預備知識前端
IO多路複用sql
swoole 中的io多路複用表現爲底層的 epoll進程模型,在C語言中表現爲 epoll 函數。thinkphp
Event loop 事件循環安全
swoole 對 epoll 實現了一個Reactor線程模型封裝,設置了read事件和write事件的監聽回調函數。(詳見swoole_event_add)微信
swoole_processswoole
咱們在php-fpm.conf配置文件中發現,php-fpm中有兩種進程池管理設置。架構
接下來用swoole代碼來實現,這裏只是爲理解swoole_process、進程間通訊、定時器等使用,實際狀況使用封裝好的swoole_server來實現task任務隊列池會更方便。微信公衆平臺
假若有個定時投遞的任務隊列:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
<?php
/**
* 動態進程池,相似fpm
* 動態新建進程
* 有初始進程數,最小進程數,進程不夠處理時候新建進程,不超過最大進程數
*/
// 一個進程定時投遞任務
/**
* 1. tick
* 2. process及其管道通信
* 3. event loop 事件循環
*/
class
processPool
{
private
$pool
;
/**
* @var swoole_process[] 記錄全部worker的process對象
*/
private
$workers
= [];
/**
* @var array 記錄worker工做狀態
*/
private
$used_workers
= [];
/**
* @var int 最小進程數
*/
private
$min_woker_num
= 5;
/**
* @var int 初始進程數
*/
private
$start_worker_num
= 10;
/**
* @var int 最大進程數
*/
private
$max_woker_num
= 20;
/**
* 進程閒置銷燬秒數
* @var int
*/
private
$idle_seconds
= 5;
/**
* @var int 當前進程數
*/
private
$curr_num
;
/**
* 閒置進程時間戳
* @var array
*/
private
$active_time
= [];
public
function
__construct()
{
$this
->pool =
new
swoole_process(
function
() {
// 循環創建worker進程
for
(
$i
= 0;
$i
<
$this
->start_worker_num;
$i
++) {
$this
->createWorker();
}
echo
'初始化進程數:'
.
$this
->curr_num . PHP_EOL;
// 每秒定時往閒置的worker的管道中投遞任務
swoole_timer_tick(1000,
function
(
$timer_id
) {
static
$count
= 0;
$count
++;
$need_create
= true;
foreach
(
$this
->used_workers
as
$pid
=>
$used
) {
if
(
$used
== 0) {
$need_create
= false;
$this
->workers[
$pid
]->write(
$count
.
' job'
);
// 標記使用中
$this
->used_workers[
$pid
] = 1;
$this
->active_time[
$pid
] = time();
break
;
}
}
foreach
(
$this
->used_workers
as
$pid
=>
$used
)
// 若是全部worker隊列都沒有閒置的,則新建一個worker來處理
if
(
$need_create
&&
$this
->curr_num <
$this
->max_woker_num) {
$new_pid
=
$this
->createWorker();
$this
->workers[
$new_pid
]->write(
$count
.
' job'
);
$this
->used_workers[
$new_pid
] = 1;
$this
->active_time[
$new_pid
] = time();
}
// 閒置超過一段時間則銷燬進程
foreach
(
$this
->active_time
as
$pid
=>
$timestamp
) {
if
((time() -
$timestamp
) >
$this
->idle_seconds &&
$this
->curr_num >
$this
->min_woker_num) {
// 銷燬該進程
if
(isset(
$this
->workers[
$pid
]) &&
$this
->workers[
$pid
]
instanceof
swoole_process) {
$this
->workers[
$pid
]->write(
'exit'
);
unset(
$this
->workers[
$pid
]);
$this
->curr_num =
count
(
$this
->workers);
unset(
$this
->used_workers[
$pid
]);
unset(
$this
->active_time[
$pid
]);
echo
"{$pid} destroyed\n"
;
break
;
}
}
}
echo
"任務{$count}/{$this->curr_num}\n"
;
if
(
$count
== 20) {
foreach
(
$this
->workers
as
$pid
=>
$worker
) {
$worker
->write(
'exit'
);
}
// 關閉定時器
swoole_timer_clear(
$timer_id
);
// 退出進程池
$this
->pool->
exit
(0);
exit
();
}
});
});
$master_pid
=
$this
->pool->start();
echo
"Master $master_pid start\n"
;
while
(
$ret
= swoole_process::wait()) {
$pid
=
$ret
[
'pid'
];
echo
"process {$pid} existed\n"
;
}
}
/**
* 建立一個新進程
* @return int 新進程的pid
*/
public
function
createWorker()
{
$worker_process
=
new
swoole_process(
function
(swoole_process
$worker
) {
// 給子進程管道綁定事件
swoole_event_add(
$worker
->pipe,
function
(
$pipe
)
use
(
$worker
) {
$data
= trim(
$worker
->read());
if
(
$data
==
'exit'
) {
$worker
->
exit
(0);
exit
();
}
echo
"{$worker->pid} 正在處理 {$data}\n"
;
sleep(5);
// 返回結果,表示空閒
$worker
->write(
"complete"
);
});
});
$worker_pid
=
$worker_process
->start();
// 給父進程管道綁定事件
swoole_event_add(
$worker_process
->pipe,
function
(
$pipe
)
use
(
$worker_process
) {
$data
= trim(
$worker_process
->read());
if
(
$data
==
'complete'
) {
// 標記爲空閒
// echo "{$worker_process->pid} 空閒了\n";
$this
->used_workers[
$worker_process
->pid] = 0;
}
});
// 保存process對象
$this
->workers[
$worker_pid
] =
$worker_process
;
// 標記爲空閒
$this
->used_workers[
$worker_pid
] = 0;
$this
->active_time[
$worker_pid
] = time();
$this
->curr_num =
count
(
$this
->workers);
return
$worker_pid
;
}
}
new
processPool();
|