IO::Event(3pm) | User Contributed Perl Documentation | IO::Event(3pm) |
IO::Event - Tied Filehandles for Nonblocking IO with Object Callbacks
use IO::Event; use IO::Event 'emulate_Event'; use IO::Event 'AnyEvent'; my $ioe = IO::Event->new($filehandle); my $ioe = IO::Event::Socket::INET->new( [ARGS] ) my $ioe = IO::Event::Socket::UNIX->new( [ARGS] ) my $timer = IO::Event->timer( [after => $seconds], interval => $seconds, cb => CODE); my $idler = IO::Event->idle( [min => $seconds], [max => $seconds], [reentrant => 0], cb => CODE); IO::Event::loop(); IO::Event::unloop_all();
IO::Event provides a object-based callback system for handling nonblocking IO. The design goal is to provide a system that just does the right thing w/o the user needing to think about it much.
All APIs are kept as simple as possible yet at the same time, all functionality is accesible if needed. Simple things are easy. Hard things are possible.
Most of the time file handling syntax will work fine: "<$filehandle>" and "print $filehandle 'stuff'".
IO::Event provides automatic buffering of output (with a callback to throttle). It provides automatic line-at-a-time input.
After initial setup, call "IO::Event::loop()".
IO::Event was originally written to use Event. IO::Event still defaults to using Event but it can now use AnyEvent or its own event loop.
Until you create your first IO::Event object, you can choose which underlying event handler to use. The default is Event. To choose an event handler, use one of the following lines, import "no_emulate_Event", "emulate_Event", or "AnyEvent".
use IO::Event 'no_emulate_Event' use IO::Event 'emulate_Event' use IO::Event 'AnyEvent'
The "no_emulate_Event" option means: use Event. The "emulate_Event" option means IO::Event should use its own event loop.
Why?
You should use AnyEvent if you want to have compatibility with other event loops. You should use "emulate_Event" if you don't need compatibility with other event loops and you have missing-event bugs when using Event. You should use Event if it works for you.
The APIs are a bit different depending on which event loop you're using.
To use Event's event loop:
use IO::Event 'no_emulate_Event';
or just:
use IO::Event
IO::Event's definition for "loop()", "timer()", "idle()" and "unloop_all()" all default to the Event version unless "emulate_Event" or "AnyEvent" have been imported. This allows you to easily switch back and forth between Event's API and the others.
To use AnyEvent's select loop, import "AnyEvent".
use IO::Event 'AnyEvent';
You can use AnyEvent's API directly or you can use IO::Event's emulated APIs: "IO::Event::loop()", "IO::Event::unloop()", "IO::Event::timer()", and "IO::Event::idle()". These behave like Event's routines of the same name but use AnyEvent underneath.
During testing, using the pure-perl event loop of AnyEvent::Impl::Perl from AnyEvent version 5.271, some read events were dropped. To work around this, a synthetic read-ready event is dispatched for all connected read filehandles every two seconds. Turn this off or adjust its frequency by changing $IO::Event::AnyEvent::lost_event_hack. A numeric value is the time (in seconds) between dispatching read events. A false value turns off this performance-sapping hack.
AnyEvent only provides basic support for idle() events: it promises to invoke them "every now and then".
To use IO::Event's own select loop, import "emulate_Event".
use IO::Event 'emulate_Event';
IO::Event does not provide a complete emulation of everything that Event does. It provides the full timer API:
my $timer = IO::Event::timer( [ARGS] )
instead of
my $timer = Event::timer( [ARGS] )
However it does not provide timer events on filehandles, nor does it provide events for signals, or variable accesses.
Use "IO::Event::loop()" instead of "Event::loop()". Use "IO::Event::unloop_all()" instead of "Event::unloop_all()". Use "IO::Event::idle()" instead of "Event::idle()". It does not provide any other methods or functions from Event. If you need them, please send a patch.
The handler is the class or object where you provide callback functions to handle IO events. It defaults to the package of the calling context.
If present, $options is a hash reference with the following possible keys:
The handler defaults as above or can be set with an additional pseudo-parameter for IO::Socket::UNIX->new(): "Handler". A description for the socket can be provided with an additional psuedo-parameter: "Description".
The handler defaults as above or can be set with an additional pseudo-parameter for IO::Socket::UNIX->new(): "Handler". A description for the socket can be provided with an additional psuedo-parameter: "Description".
These handler methods must be available in the handler object/class if the situation in which they would be called arises.
<$ioe> like IO::Handle $ioe->get() like Data::LineBuffer $ioe->read() like IO::Handle $ioe->sysread() like IO::Handle $ioe->getline() like IO::Handle $ioe->getlines() like IO::Handle $ioe->getsome() see below $ioe->ungets() like FileHandle::Unget
At end-of-file, ie_input will only be invoked once. There may or may not be data in the input buffer.
sub ie_connection { my ($pkg, $ioe) = @_; my $newfh = $ioe->accept() }
These handler methods will be called if they are defined but it is not required that they be defined.
In addition to methods described in detail below, the following methods behave like their "IO" (mostly "IO::Socket") counterparts (except for being mostly non-blocking...):
connect listen open read sysread syswrite print eof shutdown
Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are passed to underlying "Event" objects:
loop unloop and many more...
Through AUTOLOAD (see the SUBSTITUTED METHODS section) methods are passed to underlying "IO" objects:
fileno stat truncate error opened untaint and many more...
IO::Event defines its own methods too:
Supported options:
The following timer construction arguments are supported by IO::Event's emulated event loop and IO::Event's API on top of AnyEvent:
my ($object, $method) = @{$timer->{cb}} $object->$method($timer)
The following methods (from Event) are supported on timer objects: start(), again(), now(), stop(), cancel(), is_cancelled(), is_running(), is_suspended(), pending.
The following idle construction arguments are supported by IO::Event's emulated event loop and IO::Event's API on top of AnyEvent:
my ($object, $method) = @{$timer->{cb}} $object->$method();
The following methods (from Event) are supported on idle objects: start(), again(), now(), stop(), cancel(), is_cancelled(), is_running(), is_suspended(), pending.
Any method invocations that fail because the method isn't defined in IO::Event will by tried twice more: once using trying for a method on the inner (hidden) filehandle and once more trying for a method on the Event object that's used to create the select loop for this module.
This dispatch is now deprecated with the choice of event handlers.
# This is a tcp line echo server my $listener = IO::Event::Socket::INET->new( Listen => 10, Proto => 'tcp', LocalPort => 2821, ); Event::loop(); sub ie_connection { my ($pkg, $lstnr) = @_; my $client = $lstnr->accept(); printf "accepted connection from %s:%s\n", $client->peerhost, $client->peerport; } sub ie_input { my ($pkg, $client, $ibufref) = @_; print $client <$client>; }
sysread() is incompatible with eof() because eof() uses getc(). Most of the time this isn't a problem. In other words, some of the time this is a problem: lines go missing.
For this reason, IO::Event never uses sysread(). In fact, if you ask it to do a sysread() it does a read() for you instead.
On the other hand, at the current time no problems with syswrite have come to light and IO::Event uses syswrite and never any other form of write/print etc.
IO::Event keeps copies of all of its registered filehandles. If you want to close a filehandle, you'll need to actually call close on it.
The filehandle object itself is a funny kind of hash reference. If you want to use it to store your own data, you can. Please don't use hash keys that begin "ie_" or "io_" as those are the prefixes used by "IO::Event" and "IO::Socket".
The syntax is kinda funny:
${*$filehandle}{'your_hash_key'}
For a different API on top of IO::Event, see IO::Event::Callback. It uses IO::Event but provides a simpler and perhaps easier-to-use API.
The following perl modules do something that is kinda similar to what is being done here:
AnyEvent::Handle, AnyEvent::AIO, IO::AIO, IO::Multiplex, IO::NonBlocking, IO::Select Event, POE, POE::Component::Server::TCP, Net::Socket::NonBlock, Net::Server::Multiplex, NetServer::Generic
The API borrows most heavily from IO::Multiplex. IO::Event uses Event.pm and thus can be used in programs that are already using Event or POE.
Since the original writing of IO::Event, AnyEvent has been released and now AnyEvent::AIO and <AnyEvent:Handle> should be considered the only good alternatives to IO::Event.
For an example program using IO::Event, see IO::Event::rinetd which used to be included in this package.
The test suite only covers 40% of the code. The module is used by its author and seems solid.
Copyright (C) 2002-2009 David Muir Sharnoff <muir@idiom.org>. Copyright (C) 2011-2013 Google, Inc. This module may be used/copied/etc on the same terms as Perl itself.
This module is packaged for Fedora by Emmanuel Seyman <emmanuel@seyman.fr>
2022-06-14 | perl v5.34.0 |