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
| class TestProcess{
public $mpid=0;
public $max_precess=8;
public $task = array();
public $works = [];
public $swoole_table = NULL;
public $process_objs = [];
function handle($index, $worker){
while (true){
$msg = "test_preg_replace_slave";
$msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);//使用会crash
//$msg = preg_replace('/[ \r\n\t]*\n/', '', $msg);//正常
var_dump($msg);
}
}
public function __construct(){
try {
$this->swoole_table = new swoole_table(1024);
$this->swoole_table->column('index', swoole_table::TYPE_INT);
$this->swoole_table->create();
$this->mpid = posix_getpid();
}catch (\Exception $e){
Logger::error($e);
}
}
public function run(){
try{
for ($i=0; $i <$this->max_precess; $i++) {
$this->createProcess();
}
$this->runProcess();
$this->processWait();
} catch (Exception $e){
Logger::error($e);
exit();
}
}
private function runProcess(){
foreach($this->process_objs as $index => $process){
$this->works[$index]=$process->start();
}
}
public function createProcess($index=null){
$msg = "test_preg_replace_master";
$msg = preg_replace('/[ \r\n\t]*\n[ \r\n\t]*/', '', $msg);
var_dump($msg);
if(is_null($index)){
$index=$this->swoole_table->get('index');
if($index === false){
$index = 0;
}else{
$index = $index['index']+1;
}
}
$this->swoole_table->set('index',array('index'=>$index));
$process = new swoole_process(function(swoole_process $worker)use($index){
call_user_func_array(array($this, 'handle'),array($index, $worker));
}, false, false);
$this->process_objs[$index] = $process;
}
public function processWait(){
$ret = array();
while($ret = swoole_process::wait()) {
$pid = $ret['pid'];
}
}
}
try{
$obj = new TestProcess();
$obj->run();
} catch (Exception $e) {
Logger::error($e->getMessage());
}
|