io async
play

IO::Async Paul LeoNerd Evans 2010/12/04 LPW2010 Introductions - PowerPoint PPT Presentation

IO::Async Paul LeoNerd Evans 2010/12/04 LPW2010 Introductions Paul Evans = { CPAN => PEVANS, IRC => LeoNerd, } A look at the IO::Async distribution Why Asynchronise? IO operations are not instantaneous


  1. IO::Async Paul “LeoNerd” Evans 2010/12/04 LPW2010

  2. Introductions ● Paul Evans = { CPAN => “PEVANS”, IRC => “LeoNerd”, } ● A look at the IO::Async distribution

  3. Why Asynchronise? ● IO operations are not instantaneous ● Input comes from a variety of sources at often unpredictable times ● Blocking operations waste CPU time waiting when we could be doing other things ● Want to find a way to keep the CPU busy most of the time, by waiting on as many things at once as possible

  4. Threads Are Bad ● Can hide many subtle bugs. Bad code can easily look good ● Humans have a single-threaded thought process, aren't used to thinking about concurrency ● lets go shopping

  5. Nonblocking Operations ● Do not start an operation if it would block; EAGAIN ● fcntl(2) to set O_NONBLOCK flag ● Need a way to await operations that can complete ● select(2) , poll(2) ( POSIX ) ● epoll(4) ( Linux ), kqueue(2) ( BSD ), PORTS ( Solaris ) ● Notify on ready

  6. Asynchronous Operations ● Perform an operation in the background ● “Nonblocking” connect over TCP – EINPROGRESS ● aio(3) ( POSIX ) ● IO/CP ( MSWin32 ) ● DNIX ● See also: aio within kqueue , PORTS,... ● Notify on completion

  7. Callbacks and Continuations ● Use a closure as a continuation ● Captures lexical state to resume operation ● Perl has nice support for lexical closures ● See also CPS

  8. IO::Async ● Version: 0.31 ● Collection of related modules for asynchronous IO and related operations http://search.cpan.org/~pevans/IO-Async-0.31/

  9. IO::Async::Loop ● Single object that performs actual IO multiplexing operations ● An explicit object, unlike most other eventing systems ● Performs underlying blocking call ( select(2) et.al.) ● Container for Notifier objects ● “Magic” constructor

  10. Loop Subclasses ● General Purpose ● Select, Poll ● OS specific ● Ppoll, Epoll ( Linux ) ● KQueue – still under development ( BSD ) ● Tie into other event systems ● Glib

  11. IO::Async::Notifier ● Base class; subclasses actually perform IO work ● Notifier alone performs no work, but can act as a container of others. ● Notifier s form trees, sometimes lone leaves, all (indirectly) members of a Loop .

  12. IO::Async::Handle ● Contains an IO handle object ● Or two IO handle objects – reading and writing may be split ● Events on read- or write-readiness ● Usable in two ways: ● Base class – override methods ● Adapter object – provide callback parameters

  13. IO::Async::Stream ● Subclass of Handle ● Wraps a buffer around reading and writing ● on_read designed to avoid common bugs: ● partial records ● multiple records

  14. IO::Async::Timer ● Countdown – fires an event after a fixed delay ● Periodic – fires an event at regular intervals. Not subject to processing drift

  15. IO::Async::Signal and ::PID ● Multiple Signal objects can watch for one POSIX signal; no order guarantees ● PID watches for exit of a child process

  16. IO::Async::Protocol + ::Protocol::Stream ● Base-class “abstract” Notifier s ● Perform no IO, delegate such to a Stream ● Allows SSL, SOCKS, etc... ● API looks same as a Stream

  17. IO::Async::Listener ● Wraps a listen-mode socket (usually a TCP socket) ● Invokes continuation when a new connection is accept() ed. ● on_accepted , new on_stream / on_socket

  18. Other Notifiers ● DetachedCode – run a blocking call in a child process, communicate over pipes ● Socket – wrap send/recv queues for SOCK_DGRAM sockets ● Sequencer – request pipelining, associating requests with responses

  19. Loop Abilities ● connect and listen ● Nonblocking name and service resolution ● Full IPv6 support ● process management

  20. IO::Async::MergePoint ● Utility object; not a Notifier ● Merge diverged control flow ● Collect information ● Part of the scatter/gather pattern ● See also CPS::kpar

  21. External Distributions ● Generally use Net-Async-* for implementing networking protocols ● Some distributions take -Async suffix: ● IPC::PerlSSH::Async ● Term::TermKey::Async

  22. IO-Async-SSL ● Extends IO::Async::Loop and related ● Allows connect and listen to use SSL- wrapped connections in a mostly invisible way

  23. Net-Async-HTTP ● Simple HTTP user agent ● Performs requests against servers ● Body content streaming ● Supports IPv6, SSL

  24. Net-Async-IRC ● Everybody writes one :) ● Just a client connector; no fancy state logic ● Supports IPv6, SSL

  25. Net-Async-FTP ● See also: FtpMustDie ● ... but use it if you must. ● Supports IPv6, SSL

  26. Net-Async-WebSocket ● 0.02 at time of writing – almost entirely untested ● echo-server.pl does work against Chrome ● Supports IPv6, SSL

  27. FCGI-Async ● Original module, before IO::Async existed ● Can respond using ● Custom code ● HTTP::Request / HTTP::Response objects ● PSGI ● Provides Plack::Handler ● Supports IPv6, SSL

  28. Future Directions ● Process Notifier ● Server – subclass of Listener ● Debug support ● Named notifiers ● Event printing ● Aware of notifier nesting, class types ● Intelligence on when / when not to print events

  29. Future Additions ● More Loops ● POE , AnyEvent ● SIGIO ● MSWin32? ● More networking ● IMAP – tm604 ● XMPP

  30. Support ● IRC channel ● #ioasync on irc.perl.org

  31. Questions / Comments

Recommend


More recommend