More Related Content
PDF
Luigi PyData presentation PDF
PPTX
C#を始めたばかりの人へのLINQ to Objects PDF
PDF
Google Cloud ベストプラクティス:Google BigQuery 編 - 03 : パフォーマンスとコストの最適化 PDF
PDF
Domain Driven Design with the F# type System -- F#unctional Londoners 2014 PDF
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang) What's hot
PDF
SQL上級者こそ知って欲しい、なぜO/Rマッパーが重要か? PDF
PDF
PPTX
PDF
HBaseとRedisを使った100億超/日メッセージを処理するLINEのストレージ PDF
PDF
PDF
PlaySQLAlchemy: SQLAlchemy入門 PDF
PDF
PPTX
PDF
PDF
PDF
PDF
テストとリファクタリングに関する深い方法論 #wewlc_jp PDF
PDF
型プロファイラ:抽象解釈に基づくRuby 3の静的解析 PPTX
『例えば、PHPを避ける』以降PHPはどれだけ安全になったか PDF
PDF
準同型暗号の実装とMontgomery, Karatsuba, FFT の性能 Viewers also liked
PDF
PPTX
PDF
データベース屋がHyperledger Fabricを検証してみた PDF
PPTX
これからのPerlプロダクトのかたち(YAPC::Asia 2013) PDF
PDF
Plack::Request with Encoding PDF
PDF
ODP
PDF
Games::* - Perlで 「ゲーム」しよう #hokkaidopm PDF
PDF
PDF
YAPC::Asia 2013 - CPAN Testers Reports の情報を上手に使う PDF
PDF
PDF
PDF
PDF
"Ops Tools with Perl" 2012/05/12 Hokkaido.pm PDF
Similar to Perl 非同期プログラミング
PPTX
PPT
PDF
KEY
難しそうで難しくない少し難しいClojure並行処理 KEY
PDF
PDF
PPT
How to debug a perl script using gdb PPTX
Async Programming on Ruby PPT
CPANの依存モジュールをもう少し正しく検出したい PDF
PDF
毎秒2000Requestを捌くPerl製CMSの内部構造(Debianサーバ1台にて) PDF
PDF
PPTX
20150227 オープンソースカンファレンス Tokyo 2015 Spring KEY
PDF
18166746-NeverBlock-RubyKaigi2009 PDF
PDF
PDF
あんなテスト、こんなテスト(this and that about testing) More from lestrrat
PDF
Future of Tech "Conferences" PDF
ONIの世界 - ONIcon 2019 Winter PDF
Slicing, Dicing, And Linting OpenAPI PDF
Oxygen Not Includedをやるべき4つの理由 PDF
PDF
Builderscon tokyo 2018 speaker dinner PDF
GoらしいAPIを求める旅路 (Go Conference 2018 Spring) PDF
Google container builderと友だちになるまで PDF
PDF
PDF
PDF
PDF
Coding in the context era PDF
Kubernetes in 30 minutes (2017/03/10) PDF
Opening: builderscon tokyo 2016 PDF
Kubernetes in 20 minutes - HDE Monthly Technical Session 24 PDF
小規模でもGKE - DevFest Tokyo 2016 PDF
Don't Use Reflect - Go 1.7 release party 2016 PDF
PDF
On internationalcommunityrelations Recently uploaded
PDF
PDF
第21回 Gen AI 勉強会「NotebookLMで60ページ超の スライドを作成してみた」 PDF
Reiwa 7 IT Strategist Afternoon I Question-1 Ansoff's Growth Vector PDF
Reiwa 7 IT Strategist Afternoon I Question-1 3C Analysis PDF
さくらインターネットの今 法林リージョン:さくらのAIとか GPUとかイベントとか 〜2026年もバク進します!〜 PDF
Starlink Direct-to-Cell (D2C) 技術の概要と将来の展望 PDF
2025→2026宙畑ゆく年くる年レポート_100社を超える企業アンケート総まとめ!!_企業まとめ_1229_3版 PPTX
PDF
100年後の知財業界-生成AIスライドアドリブプレゼン イーパテントYouTube配信 Perl 非同期プログラミング
- 1.
非同期プログラミング
with Perl
2010/08/07
Japan Perl Association 代表理事
株式会社ライブドア
牧 大 輔 (@lestrrat)
- 2.
- 3.
普通の書き方
use strict;
use LWP::UserAgent;
my@urls = qw(
http://www.livedoor.com/
http://www.dena.jp/
http://mixi.jp/
http://www.gaiax.co.jp/
);
my $ua = LWP::UserAgent->new();
foreach my $url (@urls) {
my $res = $ua->get( $url );
...
}
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
効率よくread
読める? ソケット1
(データ来た?)
ソケット2
ソケット3
- 12.
効率よくread
ソケット1
読める? ソケット2
(データ来た?)
ソケット3
- 13.
効率よくread
ソケット1
ソケット2
読める? ソケット3
(データ来た?)
- 14.
効率よくread
ソケット1
ソケット2
読める?
来た! ソケット3
(データ来た?)
読み込め!
- 15.
- 16.
- 17.
効率よくread
読める?
来た! ソケット1
(データ来た?)
読み込め!
ソケット2
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
イベントループ
=
while ( $still_alive ) {
... 処理 ...
}
- 26.
- 27.
イベントループ キュー
コールバック 実行!
コールバック
コールバック
コールバック
このイベントを待っている
イベント発生!
コールバックがあるか確認
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
汎用API これだけ覚えていればOK
AnyEvent
デフォルト
Event
その他
Glib
POE
EV
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
I/O
my $io;
$io =AE::io $fh, $read_or_write, sub {
.... # $fhから読んだり、$fhに書いたり
undef $io;
};
注意:$fhは非同期モードに指定しておく
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
NG
{
my $timer =AE::timer 0, 1, sub {
warn “timer invoked”; # 走らない!
};
} # ここにたどり着いた時点で $timerが解放
スコープ終了。リソース解放され
$timerも解放されてしまう
- 57.
Good
my $timer;
$timer =AE::timer 0, 1, sub {
... # どこかで $timer を使うコード
undef $timer; # 明示的に解放しないと消えない
}
クロージャで使用されているため
明示的に解放されるまで生き残る
- 58.
- 59.
- 60.
#!perl
use strict;
use AnyEvent;
my$cv = AE::cv {
print "Endn";
};
my $timer;
$timer = AE::timer 10, 0, sub {
print "Waited 10 seconds!n";
undef $timer;
$cv->send;
};
$cv->recv;
- 61.
#!perl
use strict;
use AnyEvent;
my$cv = AE::cv {
print "Endn";
};
my $timer;
$timer = AE::timer 10, 0, sub {
print "Waited 10 seconds!n";
undef $timer;
$cv->send;
};
$cv->recv;
知らせ が来るまでこの次にはいかない
(→イベントループが起動)
- 62.
#!perl
use strict;
use AnyEvent;
my$cv = AE::cv {
print "Endn";
};
my $timer;
$timer = AE::timer 10, 0, sub {
print "Waited 10 seconds!n";
undef $timer;
$cv->send; タイマーが起動したら 知らせ を送る
};
$cv->recv;
- 63.
#!perl
use strict;
use AnyEvent;
my$cv = AE::cv {
print "Endn";
};
my $timer;
$timer = AE::timer 10, 0, sub {
print "Waited 10 seconds!n";
undef $timer;
$cv->send;
};
$cv->recv;
知らせ が来たら次の処理へ
(→スクリプト終了)
- 64.
#!perl
use strict;
use AnyEvent;
my$cv = AE::cv {
print "Endn";
};
my $timer;
$timer = AE::timer 10, 0, sub {
print "Waited 10 seconds!n";
undef $timer;
$cv->send;
};
$cv->recv;
- 65.
- 66.
- 67.
- 68.
- 69.
#!perl
use strict;
use AnyEvent;
my@delay = (1, 2, 5, 10);
my $cv = AE::cv { print "All timers are donen" };
foreach my $delay (@delay) {
my $timer;
$cv->begin;
$timer = AE::timer $delay, 0, sub {
print "Timer for delay = $delayn";
undef $timer;
$cv->end;
};
}
$cv->recv;
- 70.
#!perl
use strict;
use AnyEvent;
my@delay = (1, 2, 5, 10);
my $cv = AE::cv { print "All timers are donen" };
foreach my $delay (@delay) {
my $timer;
$cv->begin; フラグをあげる
$timer = AE::timer $delay, 0, sub {
print "Timer for delay = $delayn";
undef $timer;
$cv->end;
};
}
$cv->recv;
- 71.
#!perl
use strict;
use AnyEvent;
my@delay = (1, 2, 5, 10);
my $cv = AE::cv { print "All timers are donen" };
foreach my $delay (@delay) {
my $timer;
$cv->begin;
$timer = AE::timer $delay, 0, sub {
print "Timer for delay = $delayn";
undef $timer;
$cv->end; フラグを落とす
};
}
$cv->recv;
- 72.
#!perl
use strict;
use AnyEvent;
my@delay = (1, 2, 5, 10);
my $cv = AE::cv { print "All timers are donen" };
foreach my $delay (@delay) {
my $timer;
$cv->begin;
$timer = AE::timer $delay, 0, sub {
print "Timer for delay = $delayn";
undef $timer;
$cv->end;
};
}
$cv->recv; フラグが全部落ちたらお知らせ
- 73.
#!perl
use strict;
use AnyEvent;
my@delay = (1, 2, 5, 10);
my $cv = AE::cv { print "All timers are donen" };
foreach my $delay (@delay) {
my $timer;
$cv->begin;
$timer = AE::timer $delay, 0, sub {
print "Timer for delay = $delayn";
undef $timer;
$cv->end;
};
}
$cv->recv;
- 74.
3. 処理終了時の
コールバック
•何か戻り値が必要な時処理結果を待つ
•ウォッチャーを作る関数に知らせて欲
しいcondvarを渡す
- 75.
#!perl
use strict;
use AnyEvent;
subadd {
my ($x, $y, $cv) = @_;
my $timer;
$timer = AE::timer 5, 0, sub {
$cv->send( $x + $y );
undef $timer;
};
}
my $cv = AE::cv {
my $cv = shift;
my ($result) = $cv->recv;
print "Result = $resultn";
};
add( 3, 2, $cv );
$cv->recv;
- 76.
#!perl
use strict;
use AnyEvent;
subadd {
my ($x, $y, $cv) = @_;
my $timer;
$timer = AE::timer 5, 0, sub {
$cv->send( $x + $y );
undef $timer;
};
}
my $cv = AE::cv {
my $cv = shift;
my ($result) = $cv->recv;
print "Result = $resultn";
};
add( 3, 2, $cv ); 結果を受け取るコールバックを渡す
$cv->recv;
- 77.
#!perl
use strict;
use AnyEvent;
subadd {
my ($x, $y, $cv) = @_;
my $timer;
$timer = AE::timer 5, 0, sub {
$cv->send( $x + $y ); 計算結果をcondvarに渡す
undef $timer;
};
}
my $cv = AE::cv {
my $cv = shift;
my ($result) = $cv->recv;
print "Result = $resultn";
};
add( 3, 2, $cv );
$cv->recv;
- 78.
#!perl
use strict;
use AnyEvent;
subadd {
my ($x, $y, $cv) = @_;
my $timer;
$timer = AE::timer 5, 0, sub {
$cv->send( $x + $y );
undef $timer;
};
}
my $cv = AE::cv {
my $cv = shift;
my ($result) = $cv->recv; 結果を受け取る
print "Result = $resultn";
};
add( 3, 2, $cv );
$cv->recv;
- 79.
#!perl
use strict;
use AnyEvent;
subadd {
my ($x, $y, $cv) = @_;
my $timer;
$timer = AE::timer 5, 0, sub {
$cv->send( $x + $y );
undef $timer;
};
}
my $cv = AE::cv {
my $cv = shift;
my ($result) = $cv->recv;
print "Result = $resultn";
};
add( 3, 2, $cv );
$cv->recv;
- 80.
- 81.
- 82.
- 83.
use strict;
use AnyEvent;
useAnyEvent::HTTP;
my @urls = qw(
http://www.livedoor.com/
http://www.dena.jp/
http://mixi.jp/
http://www.gaiax.co.jp/
);
my $cv = AE::cv {
print "Fetched all urls!n";
};
my $guard;
foreach my $url (@urls) {
$cv->begin;
$guard = http_get $url, sub {
print "Got $urln";
undef $guard;
$cv->end;
}
}
$cv->recv;
- 84.
- 85.
ライブラリ
AnyEvent::Twitter AnyEvent::Memcached AnyEvent::FriendFeed::Realtime
AnyEvent::Twitter::Stream AnyEvent::ReverseHTTP AnyEvent::HTTP::MXHR
AnyEvent::CouchDB AnyEvent::SuperFeedr AnyEvent::AIO
AnyEvent::BDB AnyEvent::DNS AnyEvent::Beanstalk
AnyEvent::DBI AnyEvent::SNMP AnyEvent::MP
AnyEvent::Gearman AnyEvent::XMPP Cache::Memcached::AnyEvent
- 86.
- 87.
- 88.
- 89.
ちょっとずつレスポンス
use strict;
use AnyEvent;
sub{
my $env = shift;
return sub {
my $start_response = shift;
my $writer = $start_response->( [
200,
[ "Content-Type" => "text/plain" ]
]);
my $count = 1;
my $t; $t = AE::timer 2, 2, sub {
$writer->write( ($count * 2) . "秒たったよ!n");
if ($count++ == 5) {
undef $t;
$writer->close;
}
};
}
}
- 90.
用途
非同期サーバー
•接続後切断しない or •何らかのイベント待っている
•いつ来るかわからないデー •メッセージキュー、速いか遅
タを待っている いかわからないサービス等
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 98.
- 99.