Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ttodua opened this issue Jul 20, 2021 · 6 comments
Closed

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

ttodua opened this issue Jul 20, 2021 · 6 comments
Assignees

Comments

@ttodua
Copy link

ttodua commented Jul 20, 2021

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);
				...
			});
			
		});
	}

}
  1. 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
Copy link
Author

ttodua commented Jul 20, 2021

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
Copy link
Member

sy-records commented Jul 21, 2021

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

@ttodua
Copy link
Author

ttodua commented Jul 21, 2021

@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
Copy link
Member

sy-records commented Jul 21, 2021

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
Copy link
Member

twose commented Jul 22, 2021

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
Copy link
Author

ttodua commented Jul 22, 2021

Ok, I understand, many thanks!

@ttodua ttodua closed this as completed Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants