Coder Social home page Coder Social logo

utopia-coroutine's Introduction

utopia-coroutine

原生协程

$scheduler = new \Utopia\Co\Scheduler();
$dbConfig = new \Utopia\Co\Client\Mysql\Config();
$dbConfig->set(
    [
        "count"    => '2',// 连接池数量
        "host"     => '192.168.100.99',
        "user"     => 'root',
        "password" => '123456',
        "database" => 'test',
    ]
);
Db::addConnect($dbConfig);


go(function (){
    $data[] = yield Db::query('select sleep(2)');
    var_dump($data);
});
go(function (){
    echo "协程 1 开始\n";
    for ($i=0;$i<10000;$i++){
        yield co_sleep(1);
        echo "协程 1 :{$i}\n";
    }
});

go(function (){
    echo "协程 2 开始\n";
    yield co_sleep(1);
    echo "协程 2:{2222222222222222}\n";
    yield co_sleep(1);
    echo "协程 2:{3333333333333333}\n";
    echo "协程 2 结束\n";
});


/**
 * 模拟socket监听
 */
while (1){
    $hasCo = $scheduler->run();
    $timeout = $hasCo?0:null;
    /**
     * 根据是否还有协程,设置超时时间
     * stream_select($rSocks, $wSocks, $eSocks, $timeout)
     */
}

协程异步执sql

utopia-coroutine's People

Contributors

ctfang 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.