• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content

Loading…

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.

Like this presentation? Why not share!

Perl: Coro asynchronous

on

  • 3,383 views

asynchronous

asynchronous

Statistics

Views

Total Views
3,383
Views on SlideShare
3,383
Embed Views
0

Actions

Likes
2
Downloads
20
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Perl: Coro asynchronous Perl: Coro asynchronous Presentation Transcript

    • Perl: The Coro Module
      Cooperative Threads
    • 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
    • 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
    • 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
    • Beware of the Deadlock
      use Coro;
      my $wakeme = $Coro::current;
      async {
      $wakeme->ready if 0.5 < rand;
      };
      schedule;
      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)
    • Everything a thread needs: Semaphore
      use Coro;
      use Coro::Semaphore;
      my $sem = new Coro::Semaphore 0; # a locked semaphore
      async {
      print "unlocking semaphore ";
      $sem->up;
      };
      print "trying to lock semaphore ";
      $sem->down;
      print "we got it! ";
      Can be upped and downed multiple times
      Can be used with guard:my $guard = $lock->guard;
    • 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
      }
      };
    • 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
      }
    • Everything a thread needs: Sleep
      use Coro::Timer;
      async {
      say "before sleep 10";
      Coro::Timer::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
    • 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