diff --git a/src/course/event/chat_client.php b/src/course/event/chat_client.php new file mode 100644 index 0000000..e190098 --- /dev/null +++ b/src/course/event/chat_client.php @@ -0,0 +1,41 @@ +serv = new swoole_server("0.0.0.0", 9501); + $this->serv->set(array( + 'worker_num' => 1, + )); + $this->serv->on('Start', array($this, 'onStart')); + $this->serv->on('Connect', array($this, 'onConnect')); + $this->serv->on('Receive', array($this, 'onReceive')); + $this->serv->on('Close', array($this, 'onClose')); + + $this->serv->start(); + } + public function onStart( $serv ) { + echo "Start\n"; + } + public function onConnect( $serv, $fd, $from_id ) { + echo "Client {$fd} connect\n"; + } + public function onClose( $serv, $fd, $from_id ) { + echo "Client {$fd} close connection\n"; + } + + public function onReceive( swoole_server $serv, $fd, $from_id, $data ) { + echo "Get Message From Client {$fd}:{$data}\n"; + foreach ($serv->connections as $client) { + if( $fd != $client) + $serv->send($client, $data); + } + } +} +$server = new Server(); \ No newline at end of file diff --git a/src/course/event/event_loop.php b/src/course/event/event_loop.php new file mode 100644 index 0000000..a4abe2d --- /dev/null +++ b/src/course/event/event_loop.php @@ -0,0 +1,2 @@ +process = new swoole_process(array($this, 'run') , false , true); - $this->process->signal(SIGTERM, function($signo) { - echo "{$signo} shutdown.\n"; - }); - $this->process->daemon(true,true); - var_dump($this->process); + //$this->process->daemon(true,true); $this->process->start(); - + swoole_event_add($this->process->pipe, function ($pipe){ $data = $this->process->read(); echo "RECV: " . $data.PHP_EOL; @@ -29,12 +25,23 @@ public function __construct() public function run($worker) { - swoole_timer_tick(1000, function() { + swoole_timer_tick(1000, function($timer_id ) { + static $index = 0; + $index = $index + 1; $this->process->write("Hello"); + var_dump($index); + if( $index == 10 ) + { + swoole_timer_clear($timer_id); + } }); - - swoole_process::wait(); } } -new BaseProcess(); \ No newline at end of file +new BaseProcess(); +swoole_process::signal(SIGCHLD, function($sig) { + //必须为false,非阻塞模式 + while($ret = swoole_process::wait(false)) { + echo "PID={$ret['pid']}\n"; + } +}); \ No newline at end of file diff --git a/src/course/process/dynamic.php b/src/course/process/dynamic.php new file mode 100644 index 0000000..caabe9d --- /dev/null +++ b/src/course/process/dynamic.php @@ -0,0 +1,93 @@ +process = new swoole_process(array($this, 'run') , false , 2); + $this->process->start(); + + swoole_process::wait(); + } + + public function run() + { + $this->current_num = $this->min_worker_num; + + for($i=0;$i<$this->current_num ; $i++){ + $process = new swoole_process(array($this, 'task_run') , false , 2); + $pid = $process->start(); + $this->process_list[$pid] = $process; + $this->process_use[$pid] = 0; + } + foreach ($this->process_list as $process) { + swoole_event_add($process->pipe, function ($pipe) use($process) { + $data = $process->read(); + var_dump($data); + $this->process_use[$data] = 0; + }); + } + + + swoole_timer_tick(1000, function($timer_id) { + static $index = 0; + $index = $index + 1; + $flag = true; + foreach ($this->process_use as $pid => $used) { + if($used == 0) + { + $flag = false; + $this->process_use[$pid] = 1; + $this->process_list[$pid]->write($index . "Hello"); + break; + } + } + if( $flag && $this->current_num < $this->max_worker_num ) + { + $process = new swoole_process(array($this, 'task_run') , false , 2); + $pid = $process->start(); + $this->process_list[$pid] = $process; + $this->process_use[$pid] = 1; + $this->process_list[$pid]->write($index . "Hello"); + $this->current_num ++; + } + var_dump($index); + if( $index == 10 ) + { + foreach ($this->process_list as $process) { + $process->write("exit"); + } + swoole_timer_clear($timer_id); + $this->process->exit(); + } + }); + } + + public function task_run($worker) + { + swoole_event_add($worker->pipe, function ($pipe) use ($worker){ + $data = $worker->read(); + var_dump($worker->pid . ": " . $data); + if($data == 'exit') + { + $worker->exit(); + exit; + } + sleep(5); + + $worker->write("" . $worker->pid); + }); + } +} + +new BaseProcess(); diff --git a/src/course/process/msg_queue.php b/src/course/process/msg_queue.php new file mode 100644 index 0000000..b295eb8 --- /dev/null +++ b/src/course/process/msg_queue.php @@ -0,0 +1,52 @@ +process = new swoole_process(array($this, 'run') , false , true); + if( !$this->process->useQueue( 123 ) ) + { + var_dump(swoole_strerror(swoole_errno())); + exit; + } + $this->process->start(); + + while(true) + { + $data = $this->process->pop(); + echo "RECV: " . $data.PHP_EOL; + } + } + + public function run($worker) + { + swoole_timer_tick(1000, function($timer_id ) { + static $index = 0; + $index = $index + 1; + $this->process->push("Hello"); + var_dump($index); + if( $index == 10 ) + { + swoole_timer_clear($timer_id); + } + }); + } +} + +new BaseProcess(); +swoole_process::signal(SIGCHLD, function($sig) { + //必须为false,非阻塞模式 + while($ret = swoole_process::wait(false)) { + echo "PID={$ret['pid']}\n"; + } +}); \ No newline at end of file diff --git a/src/course/process/muti_process.php b/src/course/process/muti_process.php index 62361d3..898abf7 100644 --- a/src/course/process/muti_process.php +++ b/src/course/process/muti_process.php @@ -30,8 +30,18 @@ public function run() $this->process_list[$i]->start(); } - swoole_timer_tick(1000, function() { - $this->process->push("Hello"); + swoole_timer_tick(1000, function($timer_id) { + static $index = 0; + $index = $index + 1; + $this->process->push($index . "Hello"); + var_dump($index); + if( $index == 10 ) + { + $this->process->push("exit"); + $this->process->push("exit"); + $this->process->push("exit"); + swoole_timer_clear($timer_id); + } }); } @@ -41,9 +51,19 @@ public function task_run($worker) { $data = $worker->pop(); var_dump($data); + if($data == 'exit') + { + $worker->exit(); + } sleep(5); } } } -new BaseProcess(); \ No newline at end of file +new BaseProcess(); +swoole_process::signal(SIGCHLD, function($sig) { + //必须为false,非阻塞模式 + while($ret = swoole_process::wait(false)) { + echo "PID={$ret['pid']}\n"; + } +}); \ No newline at end of file