Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Achilles Xuformalin14@gmail.com
Contents•••••••
•       I/O         CPU    /         CPU•                    I/O        CPU•        /
••    •    •
• linux   win••               top    cpu
my @urllist = read_list();my @pids;for my $url (@urllist) {   my $pid = fork();   if ($pid == 0) {       print get($url); ...
my @pids;for ( 1 .. 20) {   my $pid = fork;   if ($pid == 0) {       while (1) {           my $task = lock_and_get_task();...
••   top       cpu•     perl 5.8.5   perl 5.8.8        LWP
use threads;sub load_url {    print get($_[0]);}my @urllist = read_url_list();my @tids;for my $url (@urllist) {    my $tid...
use threads;use threads::shared;my @queue : shared;for (1 .. 6) {   threads->create( sub {      while (1) {          my $t...
java.lang.Thread•    linux   windows•
• C Python   GIL• Coro       Coroutines•
Coro•   I/O      CPU•     cede     CPU•   Perl6            async
Corouse Coro;async {   # some asynchronous thread of execution   print "2n";   cede; # yield back to main   print "4n";};p...
•   I/O•••
• select• poll select• epoll               I/O•               I/O    sendfile
• POE• Twisted• AnyEvent• Java NIO
POE••              yield•   Session• $heap•
POE  sub handler_increment {    my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];    print "Session ", $session->...
Twisted•••     POE             N••                       dns connect send    request got first byte....       hook•
Twistedd = conect_to_server()d.addCallback(login_user)d.addErrback(reconnect_to_server)reactor.run()
AnyEvent•                   libevent    EV•••   Coro POE
Java NIO•   linux   windows•                     Channel
• Squid 2.x• Apache• Nginx
Squid 2.x••        sendfile•• COSS•
Apache•••       php
Nginx•• epoll   kqueue   sendfile writev••
• Questions
Upcoming SlideShare
Loading in …5
×

并发模型介绍

1,593 views

Published on

From Beijing Perl Workshop 2011 http://conference.perlchina.org/bjpw2011/

Published in: Technology
  • Be the first to comment

并发模型介绍

  1. 1. Achilles Xuformalin14@gmail.com
  2. 2. Contents•••••••
  3. 3. • I/O CPU / CPU• I/O CPU• /
  4. 4. •• • •
  5. 5. • linux win•• top cpu
  6. 6. my @urllist = read_list();my @pids;for my $url (@urllist) { my $pid = fork(); if ($pid == 0) { print get($url); exit; } elsif ($pid > 0) { push @pids, $pid; # ID }}waitpid($_, ...) for @pids; ## do other nonconcurrent jobs
  7. 7. my @pids;for ( 1 .. 20) { my $pid = fork; if ($pid == 0) { while (1) { my $task = lock_and_get_task(); do_task($task); sleep 1; } exit; } elsif ($pid > 0) { push @pids, $pid; }}waitpid($_, ...) for @pids;
  8. 8. •• top cpu• perl 5.8.5 perl 5.8.8 LWP
  9. 9. use threads;sub load_url { print get($_[0]);}my @urllist = read_url_list();my @tids;for my $url (@urllist) { my $tid = threads->create(&load_url, $url); push @tids, $tid; # ID}$_->join for @tids; # waitpid# do other things
  10. 10. use threads;use threads::shared;my @queue : shared;for (1 .. 6) { threads->create( sub { while (1) { my $task; { lock @queue; $task = shift @queue; } do_task($task); threads::sleep(1); } }}while (1) { generate_task(@queue); threads::sleep(1); }
  11. 11. java.lang.Thread• linux windows•
  12. 12. • C Python GIL• Coro Coroutines•
  13. 13. Coro• I/O CPU• cede CPU• Perl6 async
  14. 14. Corouse Coro;async { # some asynchronous thread of execution print "2n"; cede; # yield back to main print "4n";};print "1n";cede; # yield to coroprint "3n";cede; # and again
  15. 15. • I/O•••
  16. 16. • select• poll select• epoll I/O• I/O sendfile
  17. 17. • POE• Twisted• AnyEvent• Java NIO
  18. 18. POE•• yield• Session• $heap•
  19. 19. POE sub handler_increment { my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION]; print "Session ", $session->ID, "counted to ", ++$heap->{count}, ".n"; $kernel->yield(increment) if$heap->{count} < 10; }
  20. 20. Twisted••• POE N•• dns connect send request got first byte.... hook•
  21. 21. Twistedd = conect_to_server()d.addCallback(login_user)d.addErrback(reconnect_to_server)reactor.run()
  22. 22. AnyEvent• libevent EV••• Coro POE
  23. 23. Java NIO• linux windows• Channel
  24. 24. • Squid 2.x• Apache• Nginx
  25. 25. Squid 2.x•• sendfile•• COSS•
  26. 26. Apache••• php
  27. 27. Nginx•• epoll kqueue sendfile writev••
  28. 28. • Questions

×