swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.4k stars 3.16k forks source link

When is "Runtime::enableCoroutine();" needed? #4329

Closed ttodua closed 3 years ago

ttodua commented 3 years ago

I had questions about this example from official docs.

A) Is it accepted to initiate RedisPool outside of run and use that instance inside run\go ? for example:


class XYZ{

    public function init_redis()
    {
        $this->rp = new RedisPool( ... );
    }

    public function exec_swoole()
    {
        \Coroutine\run( function (){

            go(function(){
                ...
                $redis = $this->rp->get();
                $redis->set ('key', 'value');
                $this->rp->put($redis);
                ...
            });

            go(function(){
                ...
                $redis = $this->rp->get();
                $redis->set ('key', 'value');
                $this->rp->put($redis);
                ...
            });

        });
    }

}

2) On that doc-page there is shown: Runtime::enableCoroutine(); before running Coroutine\run function. Can you tell (or write on that page too), do we always need to execute that command before \run executions? I thought coroutines were enabled by default.

ttodua commented 3 years ago

Dear Swoole team, please make this enhancement: If my understanding is correct, after updating value (with ->set() function), then ->put($redis) is needed to put the updated redis instance back to pool. However, it would be nice that it was not needed to manually call ..->put($redis); in the end of every go function. It's just inconvenient.

Please make an overload of ->set() command (i.e. third flag, set to true like): $redis->set ('key', 'value', true); So, the set function (inside itself) will do ->put($redis) after updating the key&value, so we wont be required to call ->put($redis) manually in the end of every go function.

sy-records commented 3 years ago

In higher versions, when you use Coroutine\run, all HOOKs are enabled by default, so there is no need for Runtime::enableCoroutine.

ttodua commented 3 years ago

@sy-records Ok thanks for answering (your answer can be included in DOCs too) the question #2, but could you tell several words bout q1 ?

sy-records commented 3 years ago

If a connection object becomes unusable, the developer needs to call $pool->put(null); to return an empty connection to keep the connection pool balanced.

So we keep it and let the developers handle it. You can refer to simple-swoole/db

twose commented 3 years ago

Code example looks not quite good for me, Co\run approximates to main() func in C, so, the correct way to do this is that put all of your code in Co\run. About question 2, we don’t always need to recycle the connection at the end of each go func. If you want this, just create a new function to wrapper it, we only provide the minimal APIs.

ttodua commented 3 years ago

Ok, I understand, many thanks!