… | |
… | |
62 | 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 |
63 | 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 |
64 | 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 |
65 | concurrently. |
65 | concurrently. |
66 | |
66 | |
67 | While most of this works on all types of file descriptors (for example |
67 | While most of this works on all types of file descriptors (for |
68 | sockets), using these functions on file descriptors that support |
68 | example sockets), using these functions on file descriptors that |
69 | nonblocking operation (again, sockets, pipes etc.) is very inefficient or |
69 | support nonblocking operation (again, sockets, pipes etc.) is very |
70 | might not work (aio_read fails on sockets/pipes/fifos). Use an event loop |
70 | inefficient. Use an event loop for that (such as the L<Event|Event> |
71 | for that (such as the L<Event|Event> module): IO::AIO will naturally fit |
71 | module): IO::AIO will naturally fit into such an event loop itself. |
72 | into such an event loop itself. |
|
|
73 | |
72 | |
74 | In this version, a number of threads are started that execute your |
73 | In this version, a number of threads are started that execute your |
75 | requests and signal their completion. You don't need thread support |
74 | requests and signal their completion. You don't need thread support |
76 | in perl, and the threads created by this module will not be visible |
75 | in perl, and the threads created by this module will not be visible |
77 | to perl. In the future, this module might make use of the native aio |
76 | to perl. In the future, this module might make use of the native aio |
… | |
… | |
79 | not well-supported or restricted (GNU/Linux doesn't allow them on normal |
78 | not well-supported or restricted (GNU/Linux doesn't allow them on normal |
80 | files currently, for example), and they would only support aio_read and |
79 | files currently, for example), and they would only support aio_read and |
81 | aio_write, so the remaining functionality would have to be implemented |
80 | aio_write, so the remaining functionality would have to be implemented |
82 | using threads anyway. |
81 | using threads anyway. |
83 | |
82 | |
84 | Although the module will work with in the presence of other (Perl-) |
83 | Although the module will work in the presence of other (Perl-) threads, |
85 | threads, it is currently not reentrant in any way, so use appropriate |
84 | it is currently not reentrant in any way, so use appropriate locking |
86 | locking yourself, always call C<poll_cb> from within the same thread, or |
85 | yourself, always call C<poll_cb> from within the same thread, or never |
87 | never call C<poll_cb> (or other C<aio_> functions) recursively. |
86 | call C<poll_cb> (or other C<aio_> functions) recursively. |
88 | |
87 | |
89 | =head2 EXAMPLE |
88 | =head2 EXAMPLE |
90 | |
89 | |
91 | This is a simple example that uses the Event module and loads |
90 | This is a simple example that uses the Event module and loads |
92 | F</etc/passwd> asynchronously: |
91 | F</etc/passwd> asynchronously: |
… | |
… | |
328 | =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
327 | =item aio_write $fh,$offset,$length, $data,$dataoffset, $callback->($retval) |
329 | |
328 | |
330 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
329 | Reads or writes C<length> bytes from the specified C<fh> and C<offset> |
331 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
330 | into the scalar given by C<data> and offset C<dataoffset> and calls the |
332 | callback without the actual number of bytes read (or -1 on error, just |
331 | callback without the actual number of bytes read (or -1 on error, just |
333 | like the syscall). |
332 | like the syscall). If C<offset> is undefined, then the current file offset |
|
|
333 | will be used (and updated), otherwise the file offset will not be changed |
|
|
334 | by these calls. |
334 | |
335 | |
335 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
336 | The C<$data> scalar I<MUST NOT> be modified in any way while the request |
336 | is outstanding. Modifying it can result in segfaults or WW3 (if the |
337 | is outstanding. Modifying it can result in segfaults or World War III (if |
337 | necessary/optional hardware is installed). |
338 | the necessary/optional hardware is installed). |
338 | |
339 | |
339 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at |
340 | Example: Read 15 bytes at offset 7 into scalar C<$buffer>, starting at |
340 | offset C<0> within the scalar: |
341 | offset C<0> within the scalar: |
341 | |
342 | |
342 | aio_read $fh, 7, 15, $buffer, 0, sub { |
343 | aio_read $fh, 7, 15, $buffer, 0, sub { |