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

io async
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

IO::Async

Paul “LeoNerd” Evans 2010/12/04 LPW2010

slide-2
SLIDE 2

Introductions

  • Paul Evans = {

CPAN => “PEVANS”, IRC => “LeoNerd”, }

  • A look at the IO::Async distribution
slide-3
SLIDE 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
  • f the time, by waiting on as many things at
  • nce as possible
slide-4
SLIDE 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
slide-5
SLIDE 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
slide-6
SLIDE 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
slide-7
SLIDE 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
slide-8
SLIDE 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/

slide-9
SLIDE 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
slide-10
SLIDE 10

Loop Subclasses

  • General Purpose
  • Select, Poll
  • OS specific
  • Ppoll, Epoll (Linux)
  • KQueue – still under development (BSD)
  • Tie into other event systems
  • Glib
slide-11
SLIDE 11

IO::Async::Notifier

  • Base class; subclasses actually perform IO

work

  • Notifier alone performs no work, but can act

as a container of others.

  • Notifiers form trees, sometimes lone leaves,

all (indirectly) members of a Loop.

slide-12
SLIDE 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
slide-13
SLIDE 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
slide-14
SLIDE 14

IO::Async::Timer

  • Countdown – fires an event after a fixed delay
  • Periodic – fires an event at regular intervals.

Not subject to processing drift

slide-15
SLIDE 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
slide-16
SLIDE 16

IO::Async::Protocol + ::Protocol::Stream

  • Base-class “abstract” Notifiers
  • Perform no IO, delegate such to a Stream
  • Allows SSL, SOCKS, etc...
  • API looks same as a Stream
slide-17
SLIDE 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
slide-18
SLIDE 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

slide-19
SLIDE 19

Loop Abilities

  • connect and listen
  • Nonblocking name and service resolution
  • Full IPv6 support
  • process management
slide-20
SLIDE 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
slide-21
SLIDE 21

External Distributions

  • Generally use Net-Async-* for implementing

networking protocols

  • Some distributions take -Async suffix:
  • IPC::PerlSSH::Async
  • Term::TermKey::Async
slide-22
SLIDE 22

IO-Async-SSL

  • Extends IO::Async::Loop and related
  • Allows connect and listen to use SSL-

wrapped connections in a mostly invisible way

slide-23
SLIDE 23

Net-Async-HTTP

  • Simple HTTP user agent
  • Performs requests against servers
  • Body content streaming
  • Supports IPv6, SSL
slide-24
SLIDE 24

Net-Async-IRC

  • Everybody writes one :)
  • Just a client connector; no fancy state logic
  • Supports IPv6, SSL
slide-25
SLIDE 25

Net-Async-FTP

  • See also: FtpMustDie
  • ... but use it if you must.
  • Supports IPv6, SSL
slide-26
SLIDE 26

Net-Async-WebSocket

  • 0.02 at time of writing – almost entirely untested
  • echo-server.pl does work against Chrome
  • Supports IPv6, SSL
slide-27
SLIDE 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
slide-28
SLIDE 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
slide-29
SLIDE 29

Future Additions

  • More Loops
  • POE, AnyEvent
  • SIGIO
  • MSWin32?
  • More networking
  • IMAP – tm604
  • XMPP
slide-30
SLIDE 30

Support

  • IRC channel
  • #ioasync on irc.perl.org
slide-31
SLIDE 31

Questions / Comments