Skip to content

Commit cee7b94

Browse files
committed
optimized code
1 parent 81f05f4 commit cee7b94

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# ForkMan
2+
23
> A lightest process manager(inspired by [SimpleFork](https://github.com/SegmentFault/SimpleFork))
34
45
## Install
5-
```
6-
$ composer require upfor/forkman
6+
7+
```bash
8+
composer require upfor/forkman
79
```
810

911
## Example
10-
```
12+
13+
```php
1114
<?php
1215

1316
use Upfor\ForkMan\ForkMan;
@@ -26,4 +29,5 @@ $fm->master(function (ForkMan $fm) {
2629
```
2730

2831
## License
32+
2933
**ForkMan** is under the [MIT](LICENSE) license.

composer.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
"license": "MIT",
1313
"authors": [
1414
{
15-
"name": "Upfor Club",
16-
"email": "[email protected]",
17-
"homepage": "https://upfor.club"
18-
},
19-
{
20-
"name": "Shocker Li",
15+
"name": "Jioby",
2116
"email": "[email protected]",
22-
"homepage": "http://shockerli.net"
17+
"homepage": "https://shockerli.net"
2318
}
2419
],
2520
"require": {
26-
"php": ">=5.5"
21+
"php": ">=5.5",
22+
"ext-json": "*"
2723
},
2824
"autoload": {
2925
"psr-4": {

src/ForkMan.php

+28-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Upfor\ForkMan;
44

55
/**
6-
* ForkMan
6+
* ForkMan - A lightest process manager
77
*/
88
class ForkMan
99
{
@@ -69,7 +69,7 @@ public function __construct($procNum = 2, $name = '')
6969
$name = end($name);
7070
}
7171

72-
$this->name = $name;
72+
$this->name = $name;
7373
$this->procNum = $procNum;
7474

7575
if (!empty($_SERVER['argv']) && false !== array_search(static::$slaveLabel, $_SERVER['argv'])) {
@@ -80,7 +80,7 @@ public function __construct($procNum = 2, $name = '')
8080
/**
8181
* Execute only in master process
8282
*
83-
* @param callable $masterHandler
83+
* @param callable $masterHandler master process callback, which can be call_user_func() execute
8484
* @return $this
8585
*/
8686
public function master(callable $masterHandler)
@@ -100,7 +100,7 @@ public function master(callable $masterHandler)
100100
*/
101101
private function createMaster($limit)
102102
{
103-
!$this->slaveCmd && $this->slaveCmd = $this->getCmd();
103+
!$this->slaveCmd && $this->slaveCmd = $this->currentCmd();
104104

105105
@cli_set_process_title($this->name . ':' . 'master');
106106

@@ -118,11 +118,11 @@ private function createMaster($limit)
118118
*
119119
* @return string
120120
*/
121-
private function getCmd()
121+
private function currentCmd()
122122
{
123123
$prefix = empty($this->prefix) ? (!empty($_SERVER['_']) ? realpath($_SERVER['_']) : '/usr/bin/env php') : $this->prefix;
124-
$mixed = array_merge([$prefix, $_SERVER['PHP_SELF']], $_SERVER['argv']);
125-
$mixed = array_filter($mixed, function ($item) {
124+
$mixed = array_merge([$prefix, $_SERVER['PHP_SELF']], $_SERVER['argv']);
125+
$mixed = array_filter($mixed, function ($item) {
126126
return strpos($item, './') !== 0;
127127
});
128128

@@ -141,20 +141,21 @@ private function createProcess()
141141
['pipe', 'w'], // std output
142142
['pipe', 'w'], // std error
143143
];
144-
$res = proc_open($this->slaveCmd . ' ' . static::$slaveLabel, $desc, $pipes, getcwd());
144+
$res = proc_open($this->slaveCmd . ' ' . static::$slaveLabel, $desc, $pipes, getcwd());
145145

146146
$status = proc_get_status($res);
147147
if (!isset($status['pid'])) {
148148
$this->log('process create failed');
149+
149150
return $this->createProcess();
150151
}
151152

152-
$pid = $status['pid'];
153+
$pid = $status['pid'];
153154
$process = [
154-
'res' => $res,
155-
'pipes' => $pipes,
156-
'idle' => true, // process is idling
157-
'pid' => $pid,
155+
'res' => $res,
156+
'pipes' => $pipes,
157+
'idle' => true, // process is idling
158+
'pid' => $pid,
158159
'callback' => null, // call when the slave process finished
159160
];
160161

@@ -163,6 +164,7 @@ private function createProcess()
163164
stream_set_blocking($pipes[2], 0);
164165

165166
$this->log('start ' . $pid);
167+
166168
return $process;
167169
}
168170

@@ -189,7 +191,7 @@ public function log($info)
189191
/**
190192
* Execute only in slave process
191193
*
192-
* @param callable $slaveHandler
194+
* @param callable $slaveHandler slave process callback, which can be call_user_func() execute
193195
* @return $this
194196
*/
195197
public function slave(callable $slaveHandler)
@@ -211,7 +213,7 @@ private function createSlave()
211213

212214
while (true) {
213215
// listen input from master
214-
$fp = @fopen('php://stdin', 'r');
216+
$fp = @fopen('php://stdin', 'r');
215217
$recv = @fread($fp, 8); // read content length
216218
$size = intval(rtrim($recv));
217219
$data = @fread($fp, $size);
@@ -239,14 +241,15 @@ private function createSlave()
239241
public function submit($data, $callback = null)
240242
{
241243
if (!$this->isSlave) {
242-
$process = &$this->getAvailableProcess();
244+
$process = &$this->getAvailableProcess();
243245
$process['callback'] = $callback;
244-
$data = json_encode($data);
245-
$length = strlen($data);
246-
$length = str_pad($length . '', 8, ' ', STR_PAD_RIGHT);
246+
$data = json_encode($data);
247+
$length = strlen($data);
248+
$length = str_pad($length . '', 8, ' ', STR_PAD_RIGHT);
247249

248250
// send to slave process, with length and content
249251
fwrite($process['pipes'][0], $length . $data);
252+
250253
return $process['pid'];
251254
}
252255

@@ -265,13 +268,12 @@ private function &getAvailableProcess()
265268
if (isset($this->procPool[$index])) {
266269
$this->procPool[$index]['idle'] = false;
267270
$this->idleCount++;
271+
268272
return $this->procPool[$index];
269273
}
270274
// sleep 50 ms
271275
usleep(50000);
272276
}
273-
274-
return null;
275277
}
276278

277279
/**
@@ -285,7 +287,7 @@ private function check()
285287
foreach ($this->procPool as $key => &$process) {
286288
$this->checkProcessAlive($process);
287289
if (!$process['idle']) {
288-
echo stream_get_contents($process['pipes'][2]); // std error
290+
echo stream_get_contents($process['pipes'][2]); // std error
289291
$result = stream_get_contents($process['pipes'][1]); // std output
290292
if (!empty($result)) {
291293
$process['idle'] = true;
@@ -304,6 +306,7 @@ private function check()
304306
$index = $key;
305307
}
306308
}
309+
307310
return $index;
308311
}
309312

@@ -358,6 +361,7 @@ public function loop($sleep = 0)
358361
}
359362

360363
$this->check();
364+
361365
return true;
362366
}
363367

@@ -383,6 +387,7 @@ public function wait($timeout = 0)
383387
$killStatus = $this->killAllProcess();
384388
if ($killStatus) {
385389
$this->log('all slave processes exited(' . ($outed ? 'timeout' : 'idle') . ')');
390+
386391
return;
387392
}
388393
}
@@ -399,7 +404,7 @@ public function wait($timeout = 0)
399404
private function killAllProcess()
400405
{
401406
$killStatus = true;
402-
foreach ($this->procPool as &$process) {
407+
foreach ($this->procPool as $process) {
403408
$status = $this->killProcess($process);
404409
if ($status) {
405410
$this->log('kill success: ' . $process['pid']);

0 commit comments

Comments
 (0)