Skip to content

Commit b13f8f5

Browse files
author
wangxiuming
committed
done
1 parent b88c311 commit b13f8f5

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

Worker.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,10 @@ protected static function initWorkers()
592592
$worker->coroutineMessage = function($connection,$recv_buffer) use ($worker){
593593
//process request,if have commond yied push to coroutine queue
594594
$r = call_user_func($worker->onMessage, $connection, $recv_buffer);
595-
if(method_exists($r,"current")&&defined("WORKERMAN_COROUTINE_LOOP_TIME")){
596-
$r->current();//first triger coroutine
597-
//将任务加入到协程队列
598-
array_push(static::$g_coroutine_array,$r);
595+
if(method_exists($r,"current")){
596+
$hash_key = $r->current();//first triger coroutine
597+
//push to coroutine queue
598+
static::$g_coroutine_array[$hash_key] = $r;
599599
}
600600
};
601601
}
@@ -2152,9 +2152,6 @@ public function run()
21522152
exit(250);
21532153
}
21542154
}
2155-
if(defined("WORKERMAN_COROUTINE_LOOP_TIME")){
2156-
static::$globalEvent->add(WORKERMAN_COROUTINE_LOOP_TIME, EventInterface::EV_TIMER, array($this, 'coroutinesLoopController'));
2157-
}
21582155
// Main loop.
21592156
static::$globalEvent->loop();
21602157
}
@@ -2274,21 +2271,18 @@ public function acceptUdpConnection($socket)
22742271
* @param $order the coroutine order
22752272
* @return void
22762273
*/
2277-
public function coroutinesLoopController(){
2278-
static $idx = -1;
2279-
$cnt = count(static::$g_coroutine_array);
2280-
if($idx<0){
2281-
$idx = $cnt-1;
2282-
}
2283-
if($cnt>0){
2284-
$it = static::$g_coroutine_array[$idx];
2285-
if($it->valid()){
2286-
$it->next();
2287-
}else{
2288-
//coroutine done,delete coroutine from queue
2289-
array_splice(static::$g_coroutine_array,$idx,1);
2290-
}
2274+
public static function coroutinesLoopController($hash_key,$data){
2275+
2276+
2277+
$it = static::$g_coroutine_array[$hash_key];
2278+
if($it->valid()){
2279+
$_hash_key = $it->send($data);
2280+
static::$g_coroutine_array[$_hash_key]=$it;
22912281
}
2292-
$idx--;
2282+
unset(static::$g_coroutine_array[$hash_key]);
2283+
}
2284+
public static function generateCoroutineHashKey(){
2285+
static $idx = 0;
2286+
return md5(time()."_".(++$idx));
22932287
}
22942288
}

0 commit comments

Comments
 (0)