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