Skip to content

Commit 64049e5

Browse files
authored
Merge pull request #4 from RunnerLee/master
更新进程命名方法
2 parents 73f751a + 7dd5365 commit 64049e5

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

examples/process/name.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* @author jan huang <bboyjanhuang@gmail.com>
4+
* @copyright 2016
5+
*
6+
* @link https://www.github.com/janhuang
7+
* @link http://www.fast-d.cn/
8+
*/
9+
10+
use FastD\Swoole\Process;
11+
12+
include __DIR__ . '/../../vendor/autoload.php';
13+
14+
class DemoProcess extends Process
15+
{
16+
/**
17+
* Process handle
18+
*
19+
* @return callable
20+
*/
21+
public function handle(swoole_process $swoole_process)
22+
{
23+
timer_tick(1000, function ($id) {
24+
static $index = 0;
25+
$index++;
26+
echo $index . PHP_EOL;
27+
if ($index === 10) {
28+
timer_clear($id);
29+
}
30+
});
31+
}
32+
}
33+
34+
$process = new DemoProcess('fastd swoole');
35+
36+
$process->start();
37+
38+
$process->wait(function ($ret) {
39+
echo 'PID: ' . $ret['pid'];
40+
});

src/Process.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct($name = null, $callback = null, $redirect = false, $
7777

7878
$this->callback = null === $callback ? [$this, 'handle'] : $callback;
7979

80-
$this->process = new swoole_process($this->callback, $redirect, $pipe);
80+
$this->process = new swoole_process([$this, 'runProcess'], $redirect, $pipe);
8181
}
8282

8383
/**
@@ -199,9 +199,6 @@ public function exists($pid)
199199
*/
200200
public function start()
201201
{
202-
if (!empty($this->name)) {
203-
process_rename($this->name);
204-
}
205202
if (true === $this->daemonize) {
206203
$this->process->daemon();
207204
}
@@ -255,8 +252,19 @@ public function getProcess()
255252
/**
256253
* Process handle
257254
*
258-
* @param $swoole_process
255+
* @param swoole_process $swoole_process
259256
* @return callable
260257
*/
261258
public function handle(swoole_process $swoole_process){}
259+
260+
/**
261+
* @param swoole_process $worker
262+
* @return void
263+
*/
264+
public function runProcess(swoole_process $worker)
265+
{
266+
process_rename($this->name);
267+
268+
call_user_func($this->callback, $worker);
269+
}
262270
}

0 commit comments

Comments
 (0)