进程管理

Catalogue
  1. 1. @Lib/Process
  2. 2. @construct()
  3. 3. @start()
  4. 4. @$process->write($data)
  5. 5. @$process->read()
  6. 6. @$process->getpid()
  7. 7. @$process->getppid()

@Lib/Process

该扩展初始化传入回调函数并创建子进程执行,子进程间可以通过channel通讯

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$process = new Lib\Process(function(Lib\Process $process){
while(true){
$data = $process->read();
echo "child process :> get parent msg: $data \n\n";
}
});

$process->start();
for($i = 0;$i < 10 ; $i ++ ){
echo "parent process :> send child msg: $i\n";
$process->write($i);
sleep(1);
}

@construct()

初始化构造函数时必须传入回调函数,在子进程创建时会调用

@start()

执行创建子进程操作

@$process->write($data)

向子进程或者父进程写入数据

@$process->read()

向子进程或父进程读取数据

@$process->getpid()

获取当前进程id

@$process->getppid()

获取父进程id