• Like


Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Perl: Coro asynchronous




Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Perl: The Coro Module
    Cooperative Threads
  • 2. What is Coro?
    “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. Let’s see some Code
    use Coro; async { print "hello "; };
    Won’t print anything
    The main program ends without giving the coroutine chance to run
    Need to add a “cede;” in the end
  • 4. Let’s see some Code
    use Coro; async { print "async 1 "; cede; print "async 2 ";}; print "main 1 "; cede; print "main 2 "; cede;
    Will Print:
    main 1 async 1 main 2 async 2
  • 5. Beware of the Deadlock
    use Coro;
    my $wakeme = $Coro::current;
    async {
    $wakeme->ready if 0.5 < rand;
    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)
  • 6. Everything a thread needs: Semaphore
    use Coro;
    use Coro::Semaphore;
    my $sem = new Coro::Semaphore 0; # a locked semaphore
    async {
    print "unlocking semaphore ";
    print "trying to lock semaphore ";
    print "we got it! ";
    Can be upped and downed multiple times
    Can be used with guard:my $guard = $lock->guard;
  • 7. Everything a thread needs: Channel (Queue)
    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. Everything a thread needs: Non-blocking Sockets
    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 "$user ";
    print "$user@$host: $_" while <$fh>;
    print "$user@$host: done ";
    # 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. Everything a thread needs: Sleep
    use Coro::Timer;
    async {
    say "before sleep 10";
    say "after sleep 10";
    • Internally, uses AnyEvent
  • AnyEvent Integration
    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
  • 10. use Modern::Perl;
    use Coro;
    use AnyEvent;
    use AnyEvent::Impl::Perl;
    use Coro::Timer;
    for my $t (10, 20) {
    async {
    say "before 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";
    The Output:
    before loop
    before sleep 10
    before sleep 20
    it's time 5
    after sleep 10
    it's time 15
    after sleep 20