ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Util.pm
(Generate patch)

Comparing Coro/Coro/Util.pm (file contents):
Revision 1.62 by root, Fri Dec 11 18:32:23 2009 UTC vs.
Revision 1.63 by root, Fri Dec 25 07:17:12 2009 UTC

8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11This module implements various utility functions, mostly replacing perl 11This module implements various utility functions, mostly replacing perl
12functions by non-blocking counterparts. 12functions by non-blocking counterparts.
13
14Many of these functions exist for the sole purpose of emulating existing
15interfaces, no matter how bad or limited they are (e.g. no IPv6 support).
13 16
14This module is an AnyEvent user. Refer to the L<AnyEvent> 17This module is an AnyEvent user. Refer to the L<AnyEvent>
15documentation to see how to integrate it into your own programs. 18documentation to see how to integrate it into your own programs.
16 19
17=over 4 20=over 4
75 wantarray ? @r : $r[0]; 78 wantarray ? @r : $r[0];
76} 79}
77 80
78=item $ipn = Coro::Util::inet_aton $hostname || $ip 81=item $ipn = Coro::Util::inet_aton $hostname || $ip
79 82
80Works almost exactly like its AnyEvent::Socket counterpart, except that it 83Works almost exactly like its C<Socket::inet_aton> counterpart, except
81does not block other coroutines and returns the results. 84that it does not block other coroutines.
85
86Does not handle multihomed hosts or IPv6 - consider using
87C<AnyEvent::Socket::resolve_sockaddr> with the L<Coro> rouse functions
88instead.
82 89
83=cut 90=cut
84 91
85sub inet_aton { 92sub inet_aton {
86 AnyEvent::Socket::inet_aton $_[0], Coro::rouse_cb; 93 AnyEvent::Socket::inet_aton $_[0], Coro::rouse_cb;
87 my @res = Coro::rouse_wait; 94 (grep length == 4, Coro::rouse_wait)[0]
88 wantarray ? @res : $res[0]
89} 95}
90 96
91=item gethostbyname, gethostbyaddr 97=item gethostbyname, gethostbyaddr
92 98
93Work similarly to their perl counterparts, but do not block. Uses 99Work similarly to their Perl counterparts, but do not block. Uses
94C<Anyevent::Util::inet_aton> internally. 100C<AnyEvent::Util::inet_aton> internally.
101
102Does not handle multihomed hosts or IPv6 - consider using
103C<AnyEvent::Socket::resolve_sockaddr> or C<AnyEvent::DNS::reverse_lookup>
104with the L<Coro> rouse functions instead.
95 105
96=cut 106=cut
97 107
98sub gethostbyname($) { 108sub gethostbyname($) {
99 my @res = inet_aton $_[0]; 109 AnyEvent::Socket::inet_aton $_[0], Coro::rouse_cb;
100 110
101 ($_[0], $_[0], &Socket::AF_INET, 4, map +(format_ip $_), grep length == 4, @res) 111 ($_[0], $_[0], &Socket::AF_INET, 4, map +(AnyEvent::Socket::format_address $_), grep length == 4, Coro::rouse_wait)
102} 112}
103 113
104sub gethostbyaddr($$) { 114sub gethostbyaddr($$) {
105 _do_asy { gethostbyaddr $_[0], $_[1] } @_ 115 _do_asy { gethostbyaddr $_[0], $_[1] } @_
106} 116}
122C<system()>) and do not care about the overhead enough to code your own 132C<system()>) and do not care about the overhead enough to code your own
123pid watcher etc. 133pid watcher etc.
124 134
125This function might keep a pool of processes in some future version, as 135This function might keep a pool of processes in some future version, as
126fork can be rather slow in large processes. 136fork can be rather slow in large processes.
137
138You should also look at C<AnyEvent::Util::fork_eval>, which is newer and
139more compatible to totally broken Perl implementations such as the one
140from ActiveState.
127 141
128Example: execute some external program (convert image to rgba raw form) 142Example: execute some external program (convert image to rgba raw form)
129and add a long computation (extract the alpha channel) in a separate 143and add a long computation (extract the alpha channel) in a separate
130process, making sure that never more then $NUMCPUS processes are being 144process, making sure that never more then $NUMCPUS processes are being
131run. 145run.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines