undefinedfix
Sign in

Hope to add a coroutine ID to get onrequest, so as to facilitate the development of helper functions.

Rufus_12 edited in Fri, 01 Jul 2022

PHP's other frameworks are all one process processing a request. So there are a lot of short global helper functions. For example, DB model input and so on. However, it is not easy to implement assistant function when a request of swoole is completed in a coroutine. Hope to add a coroutine ID to get onrequest. This allows you to implement helper functions.

$staticReqs=array();

function getRequestID(){
    return co::getCid(); //这里希望有新的函数
}
function model(){
    global staticReqs;
    $db=$staticReqs[getRequestID()];
    $db->query(...);
    
}
$server = new Swoole\Http\Server('127.0.0.1', 9501, SWOOLE_BASE);

协程开始#1
$server->on('Request', function($request, $response) {
   $db=new Swoole\Coroutine\MySQL();
   $db->connect.....
   staticReqs[getRequestID()]=$db;
   //业务
   //处理,如果里面还有go协程的话getCid()就有得不到Request的协程ID,
    //直接调用 函数不传参数model()
   unset(staticReqs[getRequestID()]); 
});

$server->start();
3 Replies
zkuir
commented on Fri, 01 Jul 2022

Some of this function are spoolecoroutine:: getuid(); return to the current coroutine ID

Jevl
commented on Fri, 01 Jul 2022

https://wiki.swoole.com/wiki/page/871.html

wajika
commented on Sat, 02 Jul 2022

If getuid () is called in a function (not a class method), if the function is called in the subfield, the ID obtained is not the onrequest ID. Of course, if you calculate well, there is a way to get the parent coroutine ID. But there's a lot of code. It's easy to make mistakes.

lock This question has been locked and the reply function has been disabled.