\Swow\Coroutine::run(function () {
echo 1;
});
$coroutine = new \Swow\Coroutine(function () {
echo 1;
});
var_dump($coroutine);
//console
object(Swow\Coroutine)#2 (4) {
["id"]=>
int(2)
["state"]=>
string(7) "waiting"
["switches"]=>
int(0)
["elapsed"]=>
string(3) "0ms"
}
这个时候创建的协程不会马上执行,需要用$coroutine->resume();
唤醒协程,唤醒后会输出1
;
$coroutine = new \Swow\Coroutine(function () {
echo 1;
});
var_dump($coroutine);
$coroutine->resume();
object(Swow\Coroutine)#2 (4) {
["id"]=>
int(2)
["state"]=>
string(7) "waiting"
["switches"]=>
int(0)
["elapsed"]=>
string(3) "0ms"
}
1%
\Swow\Coroutine::getCurrent()
var_dump(\Swow\Coroutine::getCurrent());
//console
object(Swow\Coroutine)#1 (6) {
["id"]=>
int(1)
["role"]=>
string(4) "main"
["state"]=>
string(7) "running"
["switches"]=>
int(1)
["elapsed"]=>
string(3) "0ms"
["trace"]=>
string(178) "
#0 [internal function]: Swow\Coroutine->__debugInfo()
#1 /Users/heping/PhpWorkSpace/swow-cloud/swow/examples/coroutine/debug.php(14): var_dump(Object(Swow\Coroutine))
#2 {main}
"
}
关键字$coroutine->getStateName()
查看协程当前状态(waiting
,running
,dead
),$coroutine->isAvailable()
查看协程是否可用
var_dump(\Swow\Coroutine::getCurrent());
$coroutine = new \Swow\Coroutine(function () {
echo 1;
});
var_dump($coroutine->isAvailable());
var_dump($coroutine->getStateName());
$coroutine->resume();
object(Swow\Coroutine)#1 (6) {
["id"]=>
int(1)
["role"]=>
string(4) "main"
["state"]=>
string(7) "running"
["switches"]=>
int(1)
["elapsed"]=>
string(3) "1ms"
["trace"]=>
string(178) "
#0 [internal function]: Swow\Coroutine->__debugInfo()
#1 /Users/heping/PhpWorkSpace/swow-cloud/swow/examples/coroutine/debug.php(14): var_dump(Object(Swow\Coroutine))
#2 {main}
"
}
bool(true)
string(7) "waiting"
1%
$coroutine->getId()
var_dump(\Swow\Coroutine::getCurrent()->getId());
int(1)
\Swow\Coroutine::getMain()
$coroutine = new \Swow\Coroutine(function () {
var_dump(\Swow\Coroutine::getMain());
});
object(Swow\Coroutine)#1 (6) {
["id"]=>
int(1)
["role"]=>
string(4) "main"
["state"]=>
string(7) "waiting"
["switches"]=>
int(2)
["elapsed"]=>
string(3) "3ms"
["trace"]=>
string(116) "
#0 /Users/heping/PhpWorkSpace/swow-cloud/swow/examples/coroutine/debug.php(23): Swow\Coroutine->resume()
#1 {main}
"
}
$coroutine->getPrevious()
$coroutine = new \Swow\Coroutine(function () {
var_dump(\Swow\Coroutine::getCurrent()->getPrevious());
});
object(Swow\Coroutine)#1 (6) {
["id"]=>
int(1)
["role"]=>
string(4) "main"
["state"]=>
string(7) "waiting"
["switches"]=>
int(2)
["elapsed"]=>
string(3) "1ms"
["trace"]=>
string(116) "
#0 /Users/heping/PhpWorkSpace/swow-cloud/swow/examples/coroutine/debug.php(23): Swow\Coroutine->resume()
#1 {main}
"
}
$coroutine->getState()
返回的是int类型,具体值代表的含义看常量STATE_WAITING,STATE_RUNNING,STATE_DEAD,可以配合$coroutine->getStateName()
使用
var_dump(\Swow\Coroutine::getCurrent()->getState());
//int(2)
$coroutine->getStateName()
var_dump(\Swow\Coroutine::getCurrent()->getStateName());
string(7) "running"
$coroutine->getSwitches()
$c = Swow\Coroutine::getCurrent();
\Swow\Coroutine::run(function ()use($c){
$c->yield();
});
\Swow\Coroutine::run(function ()use($c){
$c->resume();
});
var_dump($c->getSwitches());
int(3)
$coroutine->getStartTime()
;$coroutine->getEndTime()
$coroutine->getElapsed()
;$coroutine->getElapsedAsString()
$c = Swow\Coroutine::getCurrent();
\Swow\Coroutine::run(function ()use($c){
$c->yield();
});
\Swow\Coroutine::run(function ()use($c){
$c->resume();
});
var_dump($c->getElapsed());
var_dump($c->getElapsedAsString());
int(14)
string(4) "15ms"
$coroutine->getExitStatus()
$coroutine->isAvailable()
,$coroutine->isAlive()
$coroutine->isExecuting()