Your SlideShare is downloading. ×
Perl: Coro asynchronous
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Perl: Coro asynchronous

3,261
views

Published on

asynchronous

asynchronous

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Cooperative Threads
  • 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. 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. 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.  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.  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. 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. 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. use Coro::Timer; async { say "before sleep 10"; Coro::Timer::sleep(10); say "after sleep 10"; };  Internally, uses AnyEvent
  • 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. 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

×