1.7.2 swoole_process�����ģ���ʹ��

1.7.2 swoole�����˶���̹���ģ�������PHP��pcntl�������pcntl�IJ�ͬ���ǣ�

���̼�ͨ�ţ�IPC��

�ӽ��̺͸�����֮�����ͨ���ܵ�ͨ�ţ��������ݡ�IPC�ڶ���̱���о����õ���PHP��pcntlģ��û���ṩIPC�Ĺ��ܣ����Թ����о��ޡ���swoole_process�ṩ����Щ���ܣ����ҷ�װ�˽ӿڡ�ֻ����ýӿڼ�����ɽ��̼�ͨ�š�

            $worker_num = 8;
            for($i = 0; $i < $worker_num; $i++)
            {
                $process = new swoole_process('callback_function', $redirect_stdout);
                $pid = $process->start();
                $workers[$pid] = $process;
            }
            
            function callback_function(swoole_process $worker)
            {
                //echo "Worker: start. PID=".$worker->pid."\n";
                //recv data from master
                $recv = $worker->read();
                echo "From Master: $recv\n";
            
                //send data to master
                $worker->write("hello master\n");
            
                sleep(2);
                $worker->exit(0);
            }
            

read/write 2������������ܵ��ڶ�д���ݡ��������ڿ���ͨ��write/read ���ӽ���д�룬��ȡ���ݡ�

��׼����/����ض���

swoole_process֧���˱�׼����������ض����ӽ�����echoʱ�����Զ�д��ܵ��������Ǵ�ӡ����Ļ��

�ӽ����첽

swoole_process�������ӽ��̿�����ͬ���ģ�Ҳ�������첽�ġ�

            function callback_function_async(swoole_process $worker)
            {
                //echo "Worker: start. PID=".$worker->pid."\n";
                //recv data from master
                $GLOBALS['worker'] = $worker;
                swoole_event_add($worker->pipe, function($pipe) {
                    $worker = $GLOBALS['worker'];
                    $recv = $worker->read();
            
                    echo "From Master: $recv\n";
            
                    //send data to master
                    $worker->write("hello master\n");
            
                    sleep(2);
            
                    $worker->exit(0);
                });
            }
            

���Խ��ܵ����뵽swoole_event�м���ʵ���첽�Ľ��̼�ͨ�ţ������ӽ����ڿ���ʹ��swoole_timer/swoole_client/swoole_async��Щ�첽��API������ʹ��swoole_event_addֱ�Ӳ���swoole��EventLoop��

����php-fpm/apache

php-fpm/apache�²�����ִ��fork��ԭ��ܼ�fork̫ԭʼ�ˣ����ʹ�ò���fork����ִ��php-fpm/apache���߼����Ǻ���������롣

��swoole_process�ǻ���fork�ķ�װ����ִ�д����PHP������ִ����Ϻ��ֱ�ӵ��õײ��exit���˳��ӽ��̣������˷��ա�

��php-fpm/apacheҪ����ʹ�ö���̣�fork���ĺܴ�������ÿ�����󶼴�������һ���ӽ��̣��ǻ��˷Ѻܶ�CPU�� ��php-fpm/apache�����ִ��ִ�� $worker->exit(1); ��ֹ���̡�1.7.3����Ż�����

����

swoole_process 1.7.3 ����������CPU�׺����á��ػ����̻���ʹ����Ϣ����/�����ڴ�Channel�����ԡ�