swoole 案例代码5【coroutine】

1、empty.php

<?php
go(function () {    
    try {
        echo "before\n";
        co::sleep(0.5);
        echo "after\n";
        throw new Exception('coro Exception.');
    } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    } finally {
        echo "First finally.\n";
    }
});
echo "exec file end\n";
    
    

2、redis/auth.php

<?php
go(function () {
    $redis = new Swoole\Coroutine\Redis;
    $redis->connect('127.0.0.1', 6379);
    $redis->auth('root');
    $redis->set('key', 'swoole redis work');
    var_dump($redis->get('key'));
});

3、redis/eval.php

<?php
go(function (){
    $redis = new Co\Redis;
    $redis->connect('127.0.0.1', 6379);
    $res = $redis->eval("return redis.call('get', 'key')");
    var_dump($res);
});

4、redis/request.php

<?php
go(function () {
    $redis = new Co\Redis;
    $redis->connect('127.0.0.1', 6379);
    $res = $redis->request(['object', 'encoding', 'key1']);
    var_dump($res);
});

5、select/1.php

<?php
$c1 = new chan();
//consumer first with select mode 
$num = 10;
go(function () use ($c1,$num) {
    $read_list = [$c1];
    $write_list = null;
    echo "select yield\n";
    $result = chan::select($read_list, $write_list, 2);
    echo "select resume res: ".var_export($result,1)."\n";
    if ($read_list)
    {
        foreach($read_list as $ch)
        {
            for ($i=0;$i<$num;$i++)
            {
                $ret = $ch->pop();
                echo "pop [#$i] ret:".var_export($ret,1)."\n";
            }
        }
    }
});
    
go(function () use ($c1,$num) {
    echo "push start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->push("data-$i");
        echo "push [#$i] ret:".var_export($ret,1)."\n";
    }

});
echo "main end\n";

6、select/2.php

<?php
$c1 = new chan();
//consumer first without select mode
$num = 10;
go(function () use ($c1, $num) {
    echo "pop start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->pop();
        echo "pop [#$i] ret:".var_export($ret,1)."\n";
    }
});
    
go(function () use ($c1,$num) {
    echo "push start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->push("data-$i");
        echo "push [#$i] ret:".var_export($ret,1)."\n";
    }
    
});
echo "main end\n";

7、select/3.php

<?php
$c1 = new chan(1);
//product first with select mode
$num = 10;
go(function () use ($c1,$num) {
    echo "push start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->push("data-$i");
        echo "push [#$i] ret:".var_export($ret,1)."\n";
    }
});

go(function () use ($c1,$num) {
    $read_list = [$c1];
    $write_list = null;
    echo "select yield\n";
    $result = chan::select($read_list, $write_list, 2);
    echo "select resume res: ".var_export($result,1)."\n";
    if ($read_list)
    {
        foreach($read_list as $ch)
        {
            for ($i=0;$i<$num;$i++)
            {
                $ret = $ch->pop();
                echo "pop [#$i] ret:".var_export($ret,1)."\n";
            }
        }
    }
});
echo "main end\n";

8、select/4.php

<?php
$c1 = new chan(2);
//product first without select mode
$num = 10;
go(function () use ($c1,$num) {
    echo "push start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->push("data-$i");
        echo "push [#$i] ret:".var_export($ret,1)."\n";
    }
});

go(function () use ($c1, $num) {
    echo "pop start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c1->pop();
        echo "pop [#$i] ret:".var_export($ret,1)."\n";
    }
});
echo "main end\n";
        

9、select/5.php

<?php
$c1 = new chan();
$c2 = new chan();
function fibonacci($c1, $c2)
{
    go(function () use ($c1, $c2) {
        $a = 0;
        $b = 1;
        while(1) {
            $read_list = [$c2];
            $write_list = [$c1];
            $result = chan::select($read_list, $write_list, 2);
            if ($write_list) {
                $t = $a + $b;
                $a = $b;
                $b = $t;
                $write_list[0]->push($a);
            }
            if ($read_list) {
                $ret = $read_list[0]->pop();
                if ($ret === 1) {
                    echo "quit\n";
                    return 1;
                }
            }
        }
    });
}
$num = 10;
go(function () use ($c1, $c2, $num) {
    for ($i = 0; $i < $num; $i ++) {
        $ret = $c1->pop();
        echo "fibonacci @$i $ret\n";
    }
    $c2->push(1);
});
fibonacci($c1, $c2);

10、select/6.php

<?php
$c1 = new chan();

$num = 10;
go(function () use ($c1,$num) {
    $read_list = [$c1];
    $write_list = null;
    echo "select yield\n";
    $result = chan::select($read_list, $write_list, 2);
    echo "select resume res: ".var_export($result,1)."\n";
    if ($read_list)
    {
        foreach($read_list as $ch)
        {
            for ($i=0;$i<$num;$i++)
            {
                $ret = $ch->pop();
                echo "pop [#$i] ret:".var_export($ret,1)."\n";
            }
        }
    }
});
    
go(function () use ($c1,$num) {
    echo "push start\n";
    for ($i=0;$i<$num;$i++)
    {
        if ($i == 2) {
            echo "start sleep\n";
            co:sleep(1);
            echo "end sleep\n";
        }
        $ret = $c1->push("data-$i");
        echo "push [#$i] ret:".var_export($ret,1)."\n";
    }
    
});
echo "main end\n";

11、select/7.php

<?php
//chan1 block and chan buffer
$c1 = new chan();
$c2 = new chan(10);
$num = 10;
go(function () use ($c2,$num) {
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c2->push("chan2-$i");
        echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
    }  
});
go(function () use ($c1,$num) {  
    $read_list = [$c1];
    $write_list = null;
    $result = chan::select($read_list, $write_list, 2);
    echo "select resume res: ".var_export($result,1)."\n";
    if ($read_list)
    {
        foreach($read_list as $ch)
        {
            for ($i=0;$i<$num;$i++)
            {
                $ret = $ch->pop();
                echo "chan1 pop [#$i] ret:".var_export($ret,1)."\n";
            }
        }
    }
});
    
go(function () use ($c1,$num) {
    echo "chan1 push start\n";
    for ($i=0;$i<$num;$i++)
    {
        if ($i == 2) {
            echo "start sleep\n";
            co:sleep(1);
            echo "end sleep\n";
        }
        $ret = $c1->push("chan1-$i");
        echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
    }
    
});

go(function () use ($c2,$num) {
    echo "chan2 pop start\n";
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c2->pop();
        echo "chan2 pop [#$i] ret:".var_export($ret,1)."\n";
    }    
});
echo "main end\n";

12、select/8.php

<?php
//chan1 block and chan buffer
$c1 = new chan();
$c2 = new chan(10);
$num = 10;
go(function () use ($c2,$num) {
    for ($i=0;$i<$num;$i++)
    {
        $ret = $c2->push("chan2-$i");
        echo "chan 2 push [#$i] ret:".var_export($ret,1)."\n";
    }  
});

go(function () use ($c1,$c2,$num) {
    $ori_list = $read_list = [$c1,$c2];
    $write_list = null;
    $result = chan::select($read_list, $write_list, 2);
    echo "select resume res: ".var_export($result,1)."\n";
    
    if ($ori_list)
    {        
        foreach ($ori_list as $chan => $ch)
        {
            for ($i=0;$i<$num;$i++)
            {
                $ret = $ch->pop();
                $chan_id = $chan + 1;
                echo "chan{$chan_id} pop [#$i] ret:".var_export($ret,1)."\n";
            }
        }
    }
});
    
go(function () use ($c1,$num) {
    echo "chan1 push start\n";
    for ($i=0;$i<$num;$i++)
    {
        if ($i == 2) {
            echo "start sleep\n";
            co:sleep(1);
            echo "end sleep\n";
        }
        $ret = $c1->push("chan1-$i");
        echo "chan1 push [#$i] ret:".var_export($ret,1)."\n";
    }
    
});
echo "main end\n";


文 / Admin
评论
  1. 2018-11-22
    亮亮

    小姐姐,网站写的不错,学习下你的代码。

金玉良言
loading