Giter Club home page Giter Club logo

component's Introduction

CoroutineRunner

use EasySwoole\Component\CoroutineRunner\Runner;
use Swoole\Coroutine\Scheduler;
use EasySwoole\Component\CoroutineRunner\Task;
$scheduler = new Scheduler;
$scheduler->add(function () {
    $runner = new Runner(4);
    $i = 10;
    while ($i){
        $runner->addTask(new Task(function ()use($runner,$i){
            var_dump("now is num.{$i} at time ".time());
            \co::sleep(1);

            if($i == 5){
                $runner->addTask(new Task(function (){
                    var_dump('this is task add in running');
                }));
            }

        }));
        $i--;
    }
    $runner->start();
    var_dump('task finish');
});
$scheduler->start();

PoolInterface Example1

use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\Component\Pool\TraitObjectInvoker;
use EasySwoole\Utility\Random;
use EasySwoole\Component\Pool\AbstractPoolObject;
use EasySwoole\Component\Pool\PoolObjectInterface;
use EasySwoole\Component\Pool\AbstractPool;
class test
{
    public $id;

    function __construct()
    {
        $this->id = Random::character(8);
    }

    function fuck(){
        var_dump('this is fuck at class:'.static::class.'@id:'.$this->id);
    }
}

class test2 extends test implements PoolObjectInterface
{
    function objectRestore()
    {
        var_dump('this is objectRestore at class:'.static::class.'@id:'.$this->id);
    }

    function gc()
    {
        // TODO: Implement gc() method.
    }

    function beforeUse(): bool
    {
        // TODO: Implement beforeUse() method.
        return true;
    }
}

class testPool extends AbstractPool
{

    protected function createObject()
    {
        // TODO: Implement createObject() method.
        return new test();
    }
}

class testPool2 extends AbstractPool
{

    protected function createObject()
    {
        // TODO: Implement createObject() method.
        return new test2();
    }
}



class test3 extends test
{
    use TraitObjectInvoker;
}

class test4 extends AbstractPoolObject
{
    function finalFuck()
    {
        var_dump('final fuck');
    }

    function objectRestore()
    {
        var_dump('final objectRestore');
    }
}

//cli下关闭pool的自动定时检查
PoolManager::getInstance()->getDefaultConfig()->setIntervalCheckTime(0);

go(function (){
    go(function (){
        $object = PoolManager::getInstance()->getPool(test::class)->getObj();
        $object->fuck();
        PoolManager::getInstance()->getPool(test::class)->recycleObj($object);
    });

    go(function (){
        testPool::invoke(function (test $test){
            $test->fuck();
        });
    });

    go(function (){
        testPool2::invoke(function (test2 $test){
            $test->fuck();
        });
    });

    go(function (){
        test3::invoke(function (test3 $test3){
            $test3->fuck();
        });
    });

    go(function (){
        $object = PoolManager::getInstance()->getPool(test4::class)->getObj();
        $object->finalFuck();
        PoolManager::getInstance()->getPool(test4::class)->recycleObj($object);
    });
});

PoolInterface Example2

use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\Component\Pool\AbstractPool;
use EasySwoole\Component\Pool\TraitInvoker;

class TestPool extends AbstractPool{
    function __construct(\EasySwoole\Component\Pool\PoolConf $conf)
    {
        var_dump('new TestPool');
        parent::__construct($conf);
    }

    protected function createObject()
    {
        // TODO: Implement createObject() method.
        return new \stdClass();
    }

}

class TestPool2
{
    use TraitInvoker;
    function __construct()
    {
        var_dump('new TestPool2');
    }

    function fuck()
    {
        var_dump('fuck');
    }

}

go(function (){
    PoolManager::getInstance()->registerAnonymous('test',function (){
        return new SplFixedArray();
    });
    $pool = PoolManager::getInstance()->getPool(stdClass::class);
    $pool2 = PoolManager::getInstance()->getPool(\Redis::class);
    $pool3 = PoolManager::getInstance()->getPool('test');
    $pool::invoke(function (stdClass $class){
        var_dump($class);
    });
    $pool2::invoke(function (\Redis $class){
        var_dump($class);
    });
    $pool3::invoke(function (SplFixedArray $array){
        var_dump($array);
    });

    TestPool::invoke(function (\stdClass $class){
//        var_dump($class);
    });

    $pool4 = PoolManager::getInstance()->getPool(TestPool::class);
    $pool4::invoke(function (\stdClass $class){
//        var_dump($class);
    });

    TestPool2::invoke(function ($class){
        $class->fuck();
    });
    $pool5 = PoolManager::getInstance()->getPool(TestPool2::class);
    $pool5::invoke(function (\TestPool2 $class){
//        $class->fuck();
    });

});

component's People

Contributors

ar026 avatar chenjing0521 avatar evalor avatar kajweb avatar kiss291323003 avatar player626 avatar runstp avatar tioncico avatar voocel avatar wangrunxinyes avatar willove avatar xiaocaigua avatar xuanyanwow avatar yaphats avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.