1Util(3) User Contributed Perl Documentation Util(3)
2
3
4
6 Coro::Util - various utility functions.
7
9 use Coro::Util;
10
12 This module implements various utility functions, mostly replacing perl
13 functions by non-blocking counterparts.
14
15 Many of these functions exist for the sole purpose of emulating
16 existing interfaces, no matter how bad or limited they are (e.g. no
17 IPv6 support).
18
19 This module is an AnyEvent user. Refer to the AnyEvent documentation to
20 see how to integrate it into your own programs.
21
22 $ipn = Coro::Util::inet_aton $hostname || $ip
23 Works almost exactly like its "Socket::inet_aton" counterpart,
24 except that it does not block other coroutines.
25
26 Does not handle multihomed hosts or IPv6 - consider using
27 "AnyEvent::Socket::resolve_sockaddr" with the Coro rouse functions
28 instead.
29
30 gethostbyname, gethostbyaddr
31 Work similarly to their Perl counterparts, but do not block. Uses
32 "AnyEvent::Util::inet_aton" internally.
33
34 Does not handle multihomed hosts or IPv6 - consider using
35 "AnyEvent::Socket::resolve_sockaddr" or
36 "AnyEvent::DNS::reverse_lookup" with the Coro rouse functions
37 instead.
38
39 @result = Coro::Util::fork_eval { ... }, @args
40 Executes the given code block or code reference with the given
41 arguments in a separate process, returning the results. The return
42 values must be serialisable with Coro::Storable. It may, of course,
43 block.
44
45 Note that using event handling in the sub is not usually a good
46 idea as you will inherit a mixed set of watchers from the parent.
47
48 Exceptions will be correctly forwarded to the caller.
49
50 This function is useful for pushing cpu-intensive computations into
51 a different process, for example to take advantage of multiple
52 CPU's. Its also useful if you want to simply run some blocking
53 functions (such as "system()") and do not care about the overhead
54 enough to code your own pid watcher etc.
55
56 This function might keep a pool of processes in some future
57 version, as fork can be rather slow in large processes.
58
59 You should also look at "AnyEvent::Util::fork_eval", which is newer
60 and more compatible to totally broken Perl implementations such as
61 the one from ActiveState.
62
63 Example: execute some external program (convert image to rgba raw
64 form) and add a long computation (extract the alpha channel) in a
65 separate process, making sure that never more then $NUMCPUS
66 processes are being run.
67
68 my $cpulock = new Coro::Semaphore $NUMCPUS;
69
70 sub do_it {
71 my ($path) = @_;
72
73 my $guard = $cpulock->guard;
74
75 Coro::Util::fork_eval {
76 open my $fh, "convert -depth 8 \Q$path\E rgba:"
77 or die "$path: $!";
78
79 local $/;
80 # make my eyes hurt
81 pack "C*", unpack "(xxxC)*", <$fh>
82 }
83 }
84
85 my $alphachannel = do_it "/tmp/img.png";
86
88 Marc A. Lehmann <schmorp@schmorp.de>
89 http://software.schmorp.de/pkg/Coro.html
90
91
92
93perl v5.34.0 2021-07-22 Util(3)