no threads

30
YAPC::Russia no threads;

Upload: dur-randir

Post on 07-Jul-2015

1.110 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: No threads

YAPC::Russiano threads;

Page 2: No threads

Что ждут от потоков?

• Лёгкие (память)

• Эффективный обмен данными

• Выполняются параллельно

Page 3: No threads

Что ждут от потоков?

• Лёгкие (память)

• Эффективный обмен данными

• Выполняются параллельно

Page 4: No threads

Что ждут от потоков?

• Лёгкие (память)

• Эффективный обмен данными

• Выполняются параллельно

Page 5: No threads

Что ждут от потоков?

• Лёгкие (память)

• Эффективный обмен данными

• Выполняются параллельно• Потери на синхронизацию

Page 6: No threads

Что ждут от потоков?

• Лёгкие (память)

• Эффективный обмен данными

• Выполняются параллельно• Потери на синхронизацию

• Не требуют изменений в коде?

Page 7: No threads

no Perl?

• PHP

• Python

• C++/Java/C#• Erlang

Page 8: No threads

Run me!

use threads;

binmode(STDOUT, ":encoding(ISO-8859-1)");

threads->create(sub{})->join();

Page 9: No threads

Run me!

use threads;

binmode(STDOUT, ":encoding(ISO-8859-1)");

threads->create(sub{})->join();

segmentation fault ./perltest/threaded-sysmalloc/bin/perl -e

Page 10: No threads

Run me!

use threads;

opendir(DIR, "/");

threads->create(sub{})->join();

Page 11: No threads

Run me!

use threads;

opendir(DIR, "/");

threads->create(sub{})->join();

segmentation fault ./perltest/threaded-sysmalloc/bin/perl -e

Page 12: No threads

use threads;

use Large::Module;

use Another::Large::Module;

sub foo{sleep 10}

threads->create(\&foo) for (1..100);

Run me!

Page 13: No threads

use threads;

use Large::Module;

use Another::Large::Module;

sub foo{sleep 10}

threads->create(\&foo) for (1..100); #oom, wtf???

Run me!

Page 14: No threads

use threads;

use threads::shared;

my $bar :shared;

sub foo {sleep 10}

threads->create(\&foo)->detach() for (1..10);

print BSD::Process->new->rssize;

Run me!

Page 15: No threads

use threads;

use threads::shared;

my $bar :shared;

sub foo {sleep 10}

threads->create(\&foo)->detach() for (1..10);

print BSD::Process->new->rssize; # 3276

Run me!

Page 16: No threads

use threads;

use threads::shared;

my $bar :shared;

$bar = “a”x1_000_000;

sub foo {sleep 10}

threads->create(\&foo)->detach() for (1..10);

print BSD::Process->new->rssize;

Run me!

Page 17: No threads

use threads;

use threads::shared;

my $bar :shared;

$bar = “a”x1_000_000;

sub foo {sleep 10}

threads->create(\&foo)->detach() for (1..10);

print BSD::Process->new->rssize; # 8935

Run me!

Page 18: No threads

use threads;

use threads::shared;

my $bar :shared;

$bar = “a”x1_000_000; # magic inside

sub foo {sleep 10}

threads->create(\&foo)->detach() for (1..10);

print BSD::Process->new->rssize;

Run me!

Page 19: No threads

Для чего используют потоки?

• Устранить «бутылочное горлышко»– Сети– Одного процессора

– Диска

• Уменьшить время отклика системы

Page 20: No threads

use threads;

my (@a, @b);

@a=(0..100_000);

@b=@a;

@a=sort @a;

@b=sort @b;

time ./bin/perl test_nothr.pl

0.19s user 0.02s system 99% cpu 0.208 total

Run me!

Page 21: No threads

use threads;

my (@a, @b);

@a=(0..100_000);

@b=@a;

push @thr,

threads->create(sub{@a=sort@a}),

threads->create(sub{@b=sort@b});

$_->join() for @thr;

time ./bin/perl test_thr.pl

1.18s user 0.15s system 147% cpu 0.898 total

Run me!

Page 22: No threads

use threads;

my @a :shared; my @b :shared;

@a=(0..100_000);

@b=@a;

push @thr,

threads->create(sub{@a=sort@a}),

threads->create(sub{@b=sort@b});

$_->join() for @thr;

time ./bin/perl test_thr.pl

1.31s user 0.15s system 151% cpu 0.960 total

Run me!

Page 23: No threads

use threads;

use JSON::XS;

threads->create(sub {

JSON::XS->new->….;

});

Run me!

Page 24: No threads

use threads;

#use JSON::XS;

no JSON::XS;

Run me!

Page 25: No threads

Чем заменить?

• Сеть– AnyEvent– IO::Lambda– Coro

• Диск– IO::Async– IO::AIO

• Процессор– Gearman– plain old fork

Page 26: No threads

Run me!use AnyEvent; use AnyEvent::HTTP;

sub fetch_next{ my $domain = shift @list;http_request

GET => "http://$domain/favicon.ico", recurse => 0, timeout => 30, cookie_jar => {}, headers => {Referer => "http://$domain/"}, sub { my ($data, $headers) = @_;

if ($headers->{'Status'} == 200){ #process data} fetch_next();

};

}

Page 27: No threads

Не весь CPAN одинаково полезен

package HTTP::Async;use Time::HiRes qw/sleep/;…$self->{poll_interval} = 0.05;sub _next_response {

…sleep $self->{poll_interval};

}

Page 28: No threads

use threads

• GUI

• Win

Page 29: No threads

use threads

• pre-«fork»• no use• no globals

Page 30: No threads

Вопросы?