1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
[](https://travis-ci.org/karupanerura/p5-AnyEvent-ForkManager)
# NAME
AnyEvent::ForkManager - A simple parallel processing fork manager with AnyEvent
# VERSION
This document describes AnyEvent::ForkManager version 0.07.
# SYNOPSIS
use AnyEvent;
use AnyEvent::ForkManager;
use List::Util qw/shuffle/;
my $MAX_WORKERS = 10;
my $pm = AnyEvent::ForkManager->new(max_workers => $MAX_WORKERS);
$pm->on_start(sub {
my($pm, $pid, $sec) = @_;
printf "start sleep %2d sec.\n", $sec;
});
$pm->on_finish(sub {
my($pm, $pid, $status, $sec) = @_;
printf "end sleep %2d sec.\n", $sec;
});
my @sleep_time = shuffle(1 .. 20);
foreach my $sec (@sleep_time) {
$pm->start(
cb => sub {
my($pm, $sec) = @_;
sleep $sec;
},
args => [$sec]
);
}
my $cv = AnyEvent->condvar;
# wait with non-blocking
$pm->wait_all_children(
cb => sub {
my($pm) = @_;
print "end task!\n";
$cv->send;
},
);
$cv->recv;
# DESCRIPTION
`AnyEvent::ForkManager` is much like [Parallel::ForkManager](https://metacpan.org/pod/Parallel::ForkManager),
but supports non-blocking interface with [AnyEvent](https://metacpan.org/pod/AnyEvent).
[Parallel::ForkManager](https://metacpan.org/pod/Parallel::ForkManager) is useful but,
it is difficult to use in conjunction with [AnyEvent](https://metacpan.org/pod/AnyEvent).
Because [Parallel::ForkManager](https://metacpan.org/pod/Parallel::ForkManager)'s some methods are blocking the event loop of the [AnyEvent](https://metacpan.org/pod/AnyEvent).
You can accomplish the same goals without adversely affecting the [Parallel::ForkManager](https://metacpan.org/pod/Parallel::ForkManager) to [AnyEvent::ForkManager](https://metacpan.org/pod/AnyEvent::ForkManager) with [AnyEvent](https://metacpan.org/pod/AnyEvent).
Because [AnyEvent::ForkManager](https://metacpan.org/pod/AnyEvent::ForkManager)'s methods are non-blocking the event loop of the [AnyEvent](https://metacpan.org/pod/AnyEvent).
# INTERFACE
## Methods
### `new`
This is constructor.
- max\_workers
max parallel forking count. (default: 10)
- on\_start
started child process callback.
- on\_finish
finished child process callback.
- on\_error
fork error callback.
- on\_enqueue
If push to start up child process queue, this callback is called.
- on\_dequeue
If shift from start up child process queue, this callback is called.
- on\_working\_max
If request to start up child process and process count equal max process count, this callback is called.
#### Example
my $pm = AnyEvent::ForkManager->new(
max_workers => 2, ## default 10
on_finish => sub { ## optional
my($pid, $status, @anyargs) = @_;
## this callback call when finished child process.(like AnyEvent->child)
},
on_error => sub { ## optional
my($pm, @anyargs) = @_;
## this callback call when fork failed.
},
);
### `start`
start child process.
- args
arguments passed to the callback function of the child process.
- cb
run on child process callback.
#### Example
$pm->start(
cb => sub { ## optional
my($pm, $job_id) = @_;
## this callback call in child process.
},
args => [$job_id],## this arguments passed to the callback function
);
### `wait_all_children`
You can call this method to wait for all the processes which have been forked.
This can wait with blocking or wait with non-blocking in event loop of AnyEvent.
**feature to wait with blocking is ALPHA quality till the version hits v1.0.0. Things might be broken.**
- blocking
If this parameter is true, blocking wait enable. (default: false)
**feature to wait with blocking is ALPHA quality till the version hits v1.0.0. Things might be broken.**
- cb
finished all the processes callback.
#### Example
$pm->wait_all_children(
cb => sub { ## optional
my($pm) = @_;
## this callback call when finished all child process.
},
);
### `signal_all_children`
Sends signal to all worker processes. Only usable from manager process.
### `on_error`
As a new method's argument.
### `on_start`
As a new method's argument.
### `on_finish`
As a new method's argument.
### `on_enqueue`
As a new method's argument.
### `on_dequeue`
As a new method's argument.
### `on_working_max`
As a new method's argument.
# DEPENDENCIES
Perl 5.8.1 or later.
# BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
# SEE ALSO
[AnyEvent](https://metacpan.org/pod/AnyEvent)
[AnyEvent::Util](https://metacpan.org/pod/AnyEvent::Util)
[Parallel::ForkManager](https://metacpan.org/pod/Parallel::ForkManager)
[Parallel::Prefork](https://metacpan.org/pod/Parallel::Prefork)
# AUTHOR
Kenta Sato <karupa@cpan.org>
# LICENSE AND COPYRIGHT
Copyright (c) 2012, Kenta Sato. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
|