1 | <?php |
||
17 | abstract class AbstractPool |
||
18 | { |
||
19 | /** |
||
20 | * process list |
||
21 | * |
||
22 | * @var Process[] |
||
23 | */ |
||
24 | protected $processes = array(); |
||
25 | |||
26 | /** |
||
27 | * get process by pid |
||
28 | * |
||
29 | * @param $pid |
||
30 | * @return null|Process |
||
31 | */ |
||
32 | 3 | public function getProcessByPid($pid) |
|
33 | { |
||
34 | 3 | foreach ($this->processes as $process) { |
|
35 | 3 | if ($process->getPid() == $pid) { |
|
36 | 3 | return $process; |
|
37 | } |
||
38 | } |
||
39 | |||
40 | return null; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * shutdown all process |
||
45 | * |
||
46 | * @param int $signal |
||
47 | */ |
||
48 | 9 | public function shutdown($signal = SIGTERM) |
|
49 | { |
||
50 | 9 | foreach ($this->processes as $process) { |
|
51 | 9 | if ($process->isRunning()) { |
|
52 | 9 | $process->shutdown(true, $signal); |
|
53 | 9 | } |
|
54 | 9 | } |
|
55 | 9 | } |
|
56 | |||
57 | /** |
||
58 | * shutdown sub process and no wait. it is dangerous, |
||
59 | * maybe the sub process is working. |
||
60 | */ |
||
61 | 3 | public function shutdownForce() |
|
65 | |||
66 | /** |
||
67 | * get the count of running processes |
||
68 | * |
||
69 | * @return int |
||
70 | */ |
||
71 | 21 | public function aliveCount() |
|
82 | |||
83 | /** |
||
84 | * if all processes are stopped |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | 3 | public function isFinished() |
|
97 | |||
98 | /** |
||
99 | * waiting for the sub processes to exit |
||
100 | * |
||
101 | * @param bool|true $block if true the parent process will be blocked until all |
||
102 | * sub processes exit. else it will check if thers are processes that had been exited once and return. |
||
103 | * @param int $sleep when $block is true, it will check sub processes every $sleep minute |
||
104 | */ |
||
105 | 9 | public function wait($block = true, $sleep = 100) |
|
116 | } |