swoole process write缓冲区写满测试

php环境信息如下

[root@localhost swooletest]# php --version
PHP 5.4.0 (cli) (built: Jan 15 2018 17:00:14)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Zend OPcache v7.0.4, Copyright (c) 1999-2014, by Zend Technologies
You have mail in /var/spool/mail/root
[root@localhost swooletest]# php --ri swoole
swoole
swoole support => enabled
Version => 1.9.23
Author => tianfeng.han[email: [email protected]]
epoll => enabled
eventfd => enabled
timerfd => enabled
signalfd => enabled
cpu affinity => enabled
spinlock => enabled
rwlock => enabled
async http/websocket client => enabled
Linux Native AIO => enabled
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
Directive => Local Value => Master Value
swoole.aio_thread_num => 2 => 2
swoole.display_errors => On => On
swoole.use_namespace => Off => Off
swoole.fast_serialize => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

主要为了验证 socket缓冲区写满write阻塞的极限情况,文档地址

https://wiki.swoole.com/wiki/page/372.html

id = $i;
    $pid = $process->start();
    $workers[$pid] = $process;
    simplelog("to Master: my is new worker, PID=" . $pid);
}

// 开启主进程异步模式,启动位置不能更改
master_async($workers);

// 异步主进程
function master_async($workers)
{
    
    
    // 处理子进程的信号,重新创建新的子进程
    swoole_process::signal(SIGCHLD, function ($signo) use(&$workers) {
        while (1) {
            $ret = swoole_process::wait(false);
            if ($ret) {
                $pid = $ret['pid'];
                $child_process = $workers[$pid];
                simplelog("Worker Exit, kill_signal={$ret['signal']} PID=" . $pid);
                // 重启
                $new_pid = $child_process->start();
                $workers[$new_pid] = $child_process;
                // 清除
                unset($workers[$pid]);
            } else {
                break;
            }
        }
    });
    
    $mcount = 0 ;
    // 主进程向子进程写入数据
    while ($mcount < 100000) {
        foreach ($workers as $pid => $process) {
            
                $data = str_repeat('a', 1024 );
                $process->write($mcount.$data);
                echo " write end ".$mcount."\n"  ;
                
                $mcount ++ ;
            
        }
    }
}

function child_async(swoole_process $worker)
{
    swoole_process::signal(SIGTERM, function ($signal_num) use($worker) {
        // 处理接收到的终止信号
        simplelog("signal call = $signal_num, #{$worker->pid}");
        // 正常结束
        // $worker->exit(0);
    });
    
    swoole_event_add($worker->pipe, function ($pipe) use($worker) {
       
        $recv = $worker->read();
        echo substr("xfz-->".$recv,0,20)." message len=".strlen($recv)."\n" ;
       //模拟工作耗时
        usleep(1000*20);
        
        });
}



function simplelog($message = "")
{
    echo "[" . date("Y-m-d H:i:s", time()) . "]--" . $message . "\n";
}

?>

输出信息里会出现类似如下信息,说明阻塞机制是起作用的,真实应用场景是从redis里读取数据,怕压力大时队列数据丢失.

[root@localhost swooletest]# php t.php 
//输出类似
.....
 write end 666
 write end 667
xfz-->289aaaaaaaaaaa message len=1027
xfz-->288aaaaaaaaaaa message len=1027
xfz-->291aaaaaaaaaaa message len=1027
.....

你可能感兴趣的:(swoole process write缓冲区写满测试)