… | |
… | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use IO::AIO; |
7 | use IO::AIO; |
8 | |
8 | |
9 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
9 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
10 | my ($fh) = @_; |
10 | my $fh = shift |
|
|
11 | or die "/etc/passwd: $!"; |
11 | ... |
12 | ... |
12 | }; |
13 | }; |
13 | |
14 | |
14 | aio_unlink "/tmp/file", sub { }; |
15 | aio_unlink "/tmp/file", sub { }; |
15 | |
16 | |
… | |
… | |
61 | etc.), but can also be used to easily do operations in parallel that are |
62 | etc.), but can also be used to easily do operations in parallel that are |
62 | normally done sequentially, e.g. stat'ing many files, which is much faster |
63 | normally done sequentially, e.g. stat'ing many files, which is much faster |
63 | on a RAID volume or over NFS when you do a number of stat operations |
64 | on a RAID volume or over NFS when you do a number of stat operations |
64 | concurrently. |
65 | concurrently. |
65 | |
66 | |
66 | While this works on all types of file descriptors (for example sockets), |
67 | While most of this works on all types of file descriptors (for example |
67 | using these functions on file descriptors that support nonblocking |
68 | sockets), using these functions on file descriptors that support |
68 | operation (again, sockets, pipes etc.) is very inefficient. Use an event |
69 | nonblocking operation (again, sockets, pipes etc.) is very inefficient or |
|
|
70 | might not work (aio_read fails on sockets/pipes/fifos). Use an event loop |
69 | loop for that (such as the L<Event|Event> module): IO::AIO will naturally |
71 | for that (such as the L<Event|Event> module): IO::AIO will naturally fit |
70 | fit into such an event loop itself. |
72 | into such an event loop itself. |
71 | |
73 | |
72 | In this version, a number of threads are started that execute your |
74 | In this version, a number of threads are started that execute your |
73 | requests and signal their completion. You don't need thread support |
75 | requests and signal their completion. You don't need thread support |
74 | in perl, and the threads created by this module will not be visible |
76 | in perl, and the threads created by this module will not be visible |
75 | to perl. In the future, this module might make use of the native aio |
77 | to perl. In the future, this module might make use of the native aio |
… | |
… | |
98 | poll => 'r', |
100 | poll => 'r', |
99 | cb => \&IO::AIO::poll_cb); |
101 | cb => \&IO::AIO::poll_cb); |
100 | |
102 | |
101 | # queue the request to open /etc/passwd |
103 | # queue the request to open /etc/passwd |
102 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
104 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
103 | my $fh = $_[0] |
105 | my $fh = shift |
104 | or die "error while opening: $!"; |
106 | or die "error while opening: $!"; |
105 | |
107 | |
106 | # stat'ing filehandles is generally non-blocking |
108 | # stat'ing filehandles is generally non-blocking |
107 | my $size = -s $fh; |
109 | my $size = -s $fh; |
108 | |
110 | |
… | |
… | |
188 | use strict 'vars'; |
190 | use strict 'vars'; |
189 | |
191 | |
190 | use base 'Exporter'; |
192 | use base 'Exporter'; |
191 | |
193 | |
192 | BEGIN { |
194 | BEGIN { |
193 | our $VERSION = '2.1'; |
195 | our $VERSION = '2.2'; |
194 | |
196 | |
195 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
197 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
196 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
198 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
197 | aio_fsync aio_fdatasync aio_readahead aio_rename aio_link aio_move |
199 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
198 | aio_copy aio_group aio_nop aio_mknod); |
200 | aio_move aio_copy aio_group aio_nop aio_mknod); |
199 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice)); |
200 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
201 | min_parallel max_parallel max_idle |
203 | min_parallel max_parallel max_idle |
202 | nreqs nready npending nthreads |
204 | nreqs nready npending nthreads |
203 | max_poll_time max_poll_reqs); |
205 | max_poll_time max_poll_reqs); |
… | |
… | |
416 | |
418 | |
417 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
419 | =item aio_symlink $srcpath, $dstpath, $callback->($status) |
418 | |
420 | |
419 | Asynchronously create a new symbolic link to the existing object at C<$srcpath> at |
421 | Asynchronously create a new symbolic link to the existing object at C<$srcpath> at |
420 | the path C<$dstpath> and call the callback with the result code. |
422 | the path C<$dstpath> and call the callback with the result code. |
|
|
423 | |
|
|
424 | =item aio_readlink $path, $callback->($link) |
|
|
425 | |
|
|
426 | Asynchronously read the symlink specified by C<$path> and pass it to |
|
|
427 | the callback. If an error occurs, nothing or undef gets passed to the |
|
|
428 | callback. |
421 | |
429 | |
422 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
430 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
423 | |
431 | |
424 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
432 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
425 | rename(2) and call the callback with the result code. |
433 | rename(2) and call the callback with the result code. |
… | |
… | |
964 | poll => 'r', nice => 1, |
972 | poll => 'r', nice => 1, |
965 | cb => &IO::AIO::poll_cb); |
973 | cb => &IO::AIO::poll_cb); |
966 | |
974 | |
967 | =item IO::AIO::poll_wait |
975 | =item IO::AIO::poll_wait |
968 | |
976 | |
|
|
977 | If there are any outstanding requests and none of them in the result |
969 | Wait till the result filehandle becomes ready for reading (simply does a |
978 | phase, wait till the result filehandle becomes ready for reading (simply |
970 | C<select> on the filehandle. This is useful if you want to synchronously |
979 | does a C<select> on the filehandle. This is useful if you want to |
971 | wait for some requests to finish). |
980 | synchronously wait for some requests to finish). |
972 | |
981 | |
973 | See C<nreqs> for an example. |
982 | See C<nreqs> for an example. |
974 | |
983 | |
975 | =item IO::AIO::poll |
984 | =item IO::AIO::poll |
976 | |
985 | |
977 | Waits until some requests have been handled. |
986 | Waits until some requests have been handled. |
978 | |
987 | |
|
|
988 | Returns the number of requests processed, but is otherwise strictly |
979 | Strictly equivalent to: |
989 | equivalent to: |
980 | |
990 | |
981 | IO::AIO::poll_wait, IO::AIO::poll_cb |
991 | IO::AIO::poll_wait, IO::AIO::poll_cb |
982 | if IO::AIO::nreqs; |
|
|
983 | |
992 | |
984 | =item IO::AIO::flush |
993 | =item IO::AIO::flush |
985 | |
994 | |
986 | Wait till all outstanding AIO requests have been handled. |
995 | Wait till all outstanding AIO requests have been handled. |
987 | |
996 | |