1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- class ManyPcntlInsertBigDataDepositCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'many_pcntl:insert_big_test_data';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '多进程插入测试数据';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- echo "Master process id = " . posix_getpid() . PHP_EOL;
- $i = 3;
- for ($i;$i>0;$i--){
- $pid = pcntl_fork();//创建一个新的进程
- switch ($pid) {
- case -1://如果为-1就代表失败
- die('Create failed');
- break;
- case 0://如果是0 就代表是子进程
- // Child
- echo "Child process id = " . posix_getpid() . PHP_EOL;//输出子进程的进程id
- $this->call('test:insert_big_data');
- sleep(2);
- echo "I will exit\n";
- break;
- default:
- // Parent
- if ($exit_id = pcntl_waitpid($pid, $status, WUNTRACED)) {//为了防止主进程挂了 子进程变成孤儿进程 所以在等待子进程执行完成以后
- echo "Child({$exit_id}) exited\n";
- }
- echo "Parent process id = " . posix_getpid() . PHP_EOL;
- break;
- }
- }
- }
- }
|