SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Perl : AnyEnvetと Coro の復習 サイボウズ・ラボ/Shibuya.pm 竹迫 良範 @TAKESAKO
通常の Perl プログラミング(同期) I/Oでブロック待ちが発生 use LWP::UserAgent; $ua = LWP::UserAgent->new; $req = HTTP::Request->new(         GET => "http://ll.jus.or.jp/2011/"        ); $res = $ua->request($req);      # リクエスト完了まで待つ http://ma.la/files/yapcasia2009/
AnyEvent::HTTP::http_get(非同期) #1 のリクエストが完了しないうちに#2, #3のリクエストも実行される use AnyEvent::HTTP; sub done {     print $_[0]; return $cv->done; } http_get("http://ll.jus.or.jp",amp;done); # 1 http_get("http://example.com/",amp;done); # 2 http_get("http://example.jp/", amp;done); # 3 http://ma.la/files/yapcasia2009/
AnyEvent::HTTP::http_get(実際の使い方) use AnyEvent::HTTP; my $cv = AnyEvent->condvar; sub done {     print $_[1]->{server}, "";     $cv->end; } my @url = ("http://ll.jus.or.jp/2011/",..); $cv->begin; http_get($url[0],amp;done); # 1 $cv->begin; http_get($url[1], amp;done); # 2 $cv->begin; http_get($url[2], amp;done); # 3 $cv->recv;
Coroを使うとどうなるか?   use Coro; # use Coro::LWP;   use LWP::UserAgent; async{    print "Hello, world!¥n";   };   my $ua = LWP::UserAgent->new;   my $res = $ua->get("http://ll.jus.or.jp/2011/");   print $res->title, "¥n";   cede; http://d.hatena.ne.jp/hiratara/20090921/1253537746
Coro::LWP の動作 Coroは継続の実装 + 既存関数の置き換え ブロックしそうなコードなのにブロックしない ブロックしそうな箇所で他のCoroに処理を譲る LWPで使用するIO::Socket Coro::Socketに置き換える select Coro::Select::selectに置き換える 実装は・・・ 黒魔術的? http://ma.la/files/yapcasia2009/
Coro::LWP の使い方 並列化したい箇所をasyncで囲む、これだけ  use Coro::LWP; async { $ua = LWP::UserAgent->new; $req = HTTP::Request->new( GET => "http://example.com/");  $res = $ua->request($req);  } http://ma.la/files/yapcasia2009/
Perl : AnyEventと Coro について復習 AnyEventを使った並列化 コールバックスタイルへの変更が必要 すべてのプログラムを書き直す必要がある イベント駆動に慣れたプログラマーが必要 Coroを使った並列化 継続の応用例 IO::Socket、sleep、<$fh>などを置き換え 同じ文法で並列化することが出来る 手続き指向のまま記述することができる http://ma.la/files/yapcasia2009/
POE::Component(昔流行したイベント駆動)
YAPC::Asia 2011 Tokyo チケット販売中!

Weitere ähnliche Inhalte

Mehr von takesako

x86x64 SSE4.2 POPCNT
x86x64 SSE4.2 POPCNTx86x64 SSE4.2 POPCNT
x86x64 SSE4.2 POPCNTtakesako
 
正規表現‐もう一つのバベルの塔‐木村浩一
正規表現‐もう一つのバベルの塔‐木村浩一正規表現‐もう一つのバベルの塔‐木村浩一
正規表現‐もう一つのバベルの塔‐木村浩一takesako
 
Perl6 Regex Programming with Rakudo
Perl6 Regex Programming with RakudoPerl6 Regex Programming with Rakudo
Perl6 Regex Programming with Rakudotakesako
 
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to usThat Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to ustakesako
 
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)takesako
 
Perl x86 JIT Programming
Perl x86 JIT ProgrammingPerl x86 JIT Programming
Perl x86 JIT Programmingtakesako
 
YAPC::Asia 2008 Closing Ceremony
YAPC::Asia 2008 Closing CeremonyYAPC::Asia 2008 Closing Ceremony
YAPC::Asia 2008 Closing Ceremonytakesako
 
HTML Binary Hacks & GIF89a Ployglot
HTML Binary Hacks & GIF89a PloyglotHTML Binary Hacks & GIF89a Ployglot
HTML Binary Hacks & GIF89a Ployglottakesako
 
Devsumi2008 - YAPC::Asia 2008 Tokyo
Devsumi2008 - YAPC::Asia 2008 TokyoDevsumi2008 - YAPC::Asia 2008 Tokyo
Devsumi2008 - YAPC::Asia 2008 Tokyotakesako
 
GIF89a Oldtype
GIF89a OldtypeGIF89a Oldtype
GIF89a Oldtypetakesako
 
Shibuyajs Digest
Shibuyajs DigestShibuyajs Digest
Shibuyajs Digesttakesako
 
HTML2.0 - digg - OSC2007-fall
HTML2.0 - digg - OSC2007-fallHTML2.0 - digg - OSC2007-fall
HTML2.0 - digg - OSC2007-falltakesako
 
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detection
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser DetectionShibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detection
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detectiontakesako
 
Shibuyajs24 JavaScript.GIF x LiveConnect
Shibuyajs24 JavaScript.GIF x LiveConnectShibuyajs24 JavaScript.GIF x LiveConnect
Shibuyajs24 JavaScript.GIF x LiveConnecttakesako
 

Mehr von takesako (14)

x86x64 SSE4.2 POPCNT
x86x64 SSE4.2 POPCNTx86x64 SSE4.2 POPCNT
x86x64 SSE4.2 POPCNT
 
正規表現‐もう一つのバベルの塔‐木村浩一
正規表現‐もう一つのバベルの塔‐木村浩一正規表現‐もう一つのバベルの塔‐木村浩一
正規表現‐もう一つのバベルの塔‐木村浩一
 
Perl6 Regex Programming with Rakudo
Perl6 Regex Programming with RakudoPerl6 Regex Programming with Rakudo
Perl6 Regex Programming with Rakudo
 
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to usThat Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
That Goes Without Alpha-Num (or Does It ?) all your base10 are belong to us
 
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
Devsumi2010 Ecmascript5 (ISO/IEC JTC1/SC22)
 
Perl x86 JIT Programming
Perl x86 JIT ProgrammingPerl x86 JIT Programming
Perl x86 JIT Programming
 
YAPC::Asia 2008 Closing Ceremony
YAPC::Asia 2008 Closing CeremonyYAPC::Asia 2008 Closing Ceremony
YAPC::Asia 2008 Closing Ceremony
 
HTML Binary Hacks & GIF89a Ployglot
HTML Binary Hacks & GIF89a PloyglotHTML Binary Hacks & GIF89a Ployglot
HTML Binary Hacks & GIF89a Ployglot
 
Devsumi2008 - YAPC::Asia 2008 Tokyo
Devsumi2008 - YAPC::Asia 2008 TokyoDevsumi2008 - YAPC::Asia 2008 Tokyo
Devsumi2008 - YAPC::Asia 2008 Tokyo
 
GIF89a Oldtype
GIF89a OldtypeGIF89a Oldtype
GIF89a Oldtype
 
Shibuyajs Digest
Shibuyajs DigestShibuyajs Digest
Shibuyajs Digest
 
HTML2.0 - digg - OSC2007-fall
HTML2.0 - digg - OSC2007-fallHTML2.0 - digg - OSC2007-fall
HTML2.0 - digg - OSC2007-fall
 
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detection
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser DetectionShibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detection
Shibuya.pm#8 - ImageFight - HTML 2.0 New Browser Detection
 
Shibuyajs24 JavaScript.GIF x LiveConnect
Shibuyajs24 JavaScript.GIF x LiveConnectShibuyajs24 JavaScript.GIF x LiveConnect
Shibuyajs24 JavaScript.GIF x LiveConnect
 

Kürzlich hochgeladen

SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Danieldanielhu54
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 

Kürzlich hochgeladen (10)

SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 

Node.js - Perl AnyEvent Coro

  • 1. Perl : AnyEnvetと Coro の復習 サイボウズ・ラボ/Shibuya.pm 竹迫 良範 @TAKESAKO
  • 2. 通常の Perl プログラミング(同期) I/Oでブロック待ちが発生 use LWP::UserAgent; $ua = LWP::UserAgent->new; $req = HTTP::Request->new( GET => "http://ll.jus.or.jp/2011/" ); $res = $ua->request($req); # リクエスト完了まで待つ http://ma.la/files/yapcasia2009/
  • 3. AnyEvent::HTTP::http_get(非同期) #1 のリクエストが完了しないうちに#2, #3のリクエストも実行される use AnyEvent::HTTP; sub done { print $_[0]; return $cv->done; } http_get("http://ll.jus.or.jp",amp;done); # 1 http_get("http://example.com/",amp;done); # 2 http_get("http://example.jp/", amp;done); # 3 http://ma.la/files/yapcasia2009/
  • 4. AnyEvent::HTTP::http_get(実際の使い方) use AnyEvent::HTTP; my $cv = AnyEvent->condvar; sub done { print $_[1]->{server}, ""; $cv->end; } my @url = ("http://ll.jus.or.jp/2011/",..); $cv->begin; http_get($url[0],amp;done); # 1 $cv->begin; http_get($url[1], amp;done); # 2 $cv->begin; http_get($url[2], amp;done); # 3 $cv->recv;
  • 5. Coroを使うとどうなるか? use Coro; # use Coro::LWP; use LWP::UserAgent; async{ print "Hello, world!¥n"; }; my $ua = LWP::UserAgent->new; my $res = $ua->get("http://ll.jus.or.jp/2011/"); print $res->title, "¥n"; cede; http://d.hatena.ne.jp/hiratara/20090921/1253537746
  • 6. Coro::LWP の動作 Coroは継続の実装 + 既存関数の置き換え ブロックしそうなコードなのにブロックしない ブロックしそうな箇所で他のCoroに処理を譲る LWPで使用するIO::Socket Coro::Socketに置き換える select Coro::Select::selectに置き換える 実装は・・・ 黒魔術的? http://ma.la/files/yapcasia2009/
  • 7. Coro::LWP の使い方 並列化したい箇所をasyncで囲む、これだけ use Coro::LWP; async { $ua = LWP::UserAgent->new; $req = HTTP::Request->new( GET => "http://example.com/"); $res = $ua->request($req); } http://ma.la/files/yapcasia2009/
  • 8. Perl : AnyEventと Coro について復習 AnyEventを使った並列化 コールバックスタイルへの変更が必要 すべてのプログラムを書き直す必要がある イベント駆動に慣れたプログラマーが必要 Coroを使った並列化 継続の応用例 IO::Socket、sleep、<$fh>などを置き換え 同じ文法で並列化することが出来る 手続き指向のまま記述することができる http://ma.la/files/yapcasia2009/
  • 10. YAPC::Asia 2011 Tokyo チケット販売中!