O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

并发模型介绍

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Nginx-lua
Nginx-lua
Carregando em…3
×

Confira estes a seguir

1 de 28 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (20)

Semelhante a 并发模型介绍 (20)

Anúncio

Mais recentes (20)

并发模型介绍

  1. 1. Achilles Xu formalin14@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. Coro use Coro; async { # some asynchronous thread of execution print "2n"; cede; # yield back to main print "4n"; }; print "1n"; cede; # yield to coro print "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. Twisted d = 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

×