Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

sebk69
sebk69

Posted on • Edited on

     

Let's demystify swoole : coroutines

Introduction

Swoole handle async task via a coroutine system.

Coroutines are piece of code that can be executed in parallel.

Our first async process

In that example, we will connect to yahoo and google to get html content synchronously :

#!/usr/bin/php<?php$start=microtime(true);\Co\run(function(){// Download yahoo$client=new\Swoole\Coroutine\Http\Client('www.yahoo.fr',80,true);$client->get('/');$yahooHtml=$client->getBody();$client->close();// Download google$client=new\Swoole\Coroutine\Http\Client('www.google.fr',80,true);$client->get('/');$yahooHtml=$client->getBody();$client->close();});echomicrotime(true);
Enter fullscreen modeExit fullscreen mode

This code is not async and google wait that yahoo is done. On my computer, I get a result in 1.1160490512848 seconds

Note that the \Co\run is the coroutine context, also named coroutine container.

Now, we wan't to parallelize the two requests. Just add "go" function to create coroutines :

#!/usr/bin/php<?php$start=microtime(true);\Co\run(function(){// Download yahoo in coroutine\Co\go(function(){$client=new\Swoole\Coroutine\Http\Client('www.yahoo.fr',80,false);$client->get('/');$yahooHtml=$client->getBody();$client->close();});// Download google in another coroutine\Co\go(function(){$client=new\Swoole\Coroutine\Http\Client('www.google.fr',80,false);$client->get('/');$yahooHtml=$client->getBody();$client->close();});});echomicrotime(true)-$start;
Enter fullscreen modeExit fullscreen mode

In couroutines, the jobs are done async, which mean that yahoo and google run at the same time. Then the result in my computer is now 0.61365509033203 seconds

Communication between coroutines

Now we want to communicate between coroutines. This is the role of channel :

$channel=new\Co\Channel();
Enter fullscreen modeExit fullscreen mode

We can push message :

$channel->push('hello');
Enter fullscreen modeExit fullscreen mode

And pull message :

$greeting=$channel->pop();
Enter fullscreen modeExit fullscreen mode

Put it together, notify user when download complete, got :

#!/usr/bin/php<?php\Co\run(function(){// Create channel$channel=new\Co\Channel();// Async download yahoo\Co\go(function()use($channel){$client=new\Swoole\Coroutine\Http\Client('www.yahoo.fr',80,false);$client->get('/');$yahooHtml=$client->getBody();$client->close();$channel->push('yahoo done');});// Async download google\Co\go(function()use($channel){$client=new\Swoole\Coroutine\Http\Client('www.google.fr',80,false);$client->get('/');$yahooHtml=$client->getBody();$client->close();$channel->push('google done');});// Wait and notify user\Co\go(function()use($channel){for($i=0;$i<2;$i++){echo$channel->pop()."\n";}});});
Enter fullscreen modeExit fullscreen mode

Other posts on swoole

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A passionate dev that is interested by architecture and Swoole
  • Location
    Avignon, France
  • Work
    Dev
  • Joined

More fromsebk69

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp