Your SlideShare is downloading. ×
0
Cooperative Threads
 “First class continuations called Coroutines”
 A pseudo-threads suite implemented on Perl
 Only one thread is running ...
Won’t print anything
The main program ends without giving the
coroutine chance to run
Need to add a “cede;” in the end
...
use Coro;
async {
print "async 1n";
cede;
print "async 2n";
};
print "main 1n";
cede;
print "main 2n";
cede;
Will Print:
m...
 If the test inside the async block fails, we will get the following
messge:
• FATAL: deadlock detected at - line 0
 Mai...
 Can be upped and downed
multiple times
 Can be used with guard:
my $guard = $lock->guard;
use Coro;
use Coro::Semaphore...
use Coro;
use Coro::Semaphore;
my $calculate = new Coro::Channel;
my $result = new Coro::Channel;
async {
while (1) {
my $...
use Coro;
use Coro::Socket;
sub finger {
my ($user, $host) = @_;
my $fh = new Coro::Socket PeerHost => $host, PeerPort => ...
use Coro::Timer;
async {
say "before sleep 10";
Coro::Timer::sleep(10);
say "after sleep 10";
};
 Internally, uses AnyEve...
For using AnyEvent, you need event loop
• Can put it in the main thread, or in async block
When both Coro and AnyEvent a...
use Modern::Perl;
use Coro;
use AnyEvent;
use AnyEvent::Impl::Perl;
use Coro::Timer;
$|=1;
for my $t (10, 20) {
async {
sa...
Upcoming SlideShare
Loading in...5
×

Perl: Coro asynchronous

3,398

Published on

asynchronous

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,398
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Perl: Coro asynchronous "

  1. 1. Cooperative Threads
  2. 2.  “First class continuations called Coroutines”  A pseudo-threads suite implemented on Perl  Only one thread is running in any moment Why do we need it?  Because “real” threads are difficult  And “real” Perl threads are not threads at all – almost processes  Integration with AnyEvent
  3. 3. Won’t print anything The main program ends without giving the coroutine chance to run Need to add a “cede;” in the end use Coro; async { print "hellon"; };
  4. 4. use Coro; async { print "async 1n"; cede; print "async 2n"; }; print "main 1n"; cede; print "main 2n"; cede; Will Print: main 1 async 1 main 2 async 2
  5. 5.  If the test inside the async block fails, we will get the following messge: • FATAL: deadlock detected at - line 0  Main program gave up the CPU, and is not in ready mode  Async block ended already  No runnable code – oops.  What if we want to wait for something to happen? (AnyEvent – Later) use Coro; my $wakeme = $Coro::current; async { $wakeme->ready if 0.5 < rand; }; schedule;
  6. 6.  Can be upped and downed multiple times  Can be used with guard: my $guard = $lock->guard; use Coro; use Coro::Semaphore; my $sem = new Coro::Semaphore 0; # a locked semaphore async { print "unlocking semaphoren"; $sem->up; }; print "trying to lock semaphoren"; $sem->down; print "we got it!n";
  7. 7. use Coro; use Coro::Semaphore; my $calculate = new Coro::Channel; my $result = new Coro::Channel; async { while (1) { my $num = $calculate->get; # read a number $num **= 2; # square it $result->put ($num); # put the result into the result queue } };
  8. 8. use Coro; use Coro::Socket; sub finger { my ($user, $host) = @_; my $fh = new Coro::Socket PeerHost => $host, PeerPort => "finger" or die "$user@$host: $!"; print $fh "$usern"; print "$user@$host: $_" while <$fh>; print "$user@$host: donen"; } # now finger a few accounts for ( (async { finger "abc", "cornell.edu" }), (async { finger "sebbo", "world.std.com" }), (async { finger "trouble", "noc.dfn.de" }) ) { $_->join; # wait for the result }
  9. 9. use Coro::Timer; async { say "before sleep 10"; Coro::Timer::sleep(10); say "after sleep 10"; };  Internally, uses AnyEvent
  10. 10. For using AnyEvent, you need event loop • Can put it in the main thread, or in async block When both Coro and AnyEvent are loaded, Coro::AnyEvent is automatically loaded
  11. 11. use Modern::Perl; use Coro; use AnyEvent; use AnyEvent::Impl::Perl; use Coro::Timer; $|=1; for my $t (10, 20) { async { say "before sleep $t"; Coro::Timer::sleep($t); say "after sleep $t"; }; } my $t1 = AE::timer 5, 0, sub {say "it's time 5"}; my $t2 = AE::timer 15, 0, sub {say "it's time 15"}; say "before loop"; AnyEvent->condvar->recv; The Output: before loop before sleep 10 before sleep 20 it's time 5 after sleep 10 it's time 15 after sleep 20
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×