1CPS::Governor::DeferredU(s3e)r Contributed Perl DocumentaCtPiSo:n:Governor::Deferred(3)
2
3
4
6 "CPS::Governor::Deferred" - iterate at some later point
7
9 use CPS qw( gkforeach );
10 use CPS::Governor::Deferred;
11
12 my $gov = CPS::Governor::Deferred->new;
13
14 gkforeach( $gov, [ 1 .. 10 ],
15 sub {
16 my ( $item, $knext ) = @_;
17
18 print "A$item ";
19 goto &$knext;
20 },
21 sub {},
22 );
23
24 gkforeach( $gov, [ 1 .. 10 ],
25 sub {
26 my ( $item, $knext ) = @_;
27
28 print "B$item ";
29 goto &$knext;
30 },
31 sub {},
32 );
33
34 $gov->flush;
35
37 This CPS::Governor allows the functions using it to delay their
38 iteration until some later point when the containing program invokes
39 it. This allows two main advantages:
40
41 · CPU-intensive operations may be split apart and mixed with other IO
42 operations
43
44 · Multiple control functions may be executed in pseudo-parallel,
45 interleaving iterations of each giving a kind of concurrency
46
47 These are achieved by having the governor store a list of code
48 references that need to be invoked, rather than invoking them
49 immediately. These references can then be invoked later, perhaps by
50 using an idle watcher in an event framework.
51
52 Because each code reference hasn't yet been invoked by the time the
53 "again" method is called, the original caller is free to store more
54 pending references with the governor. This allows multiple control
55 functions to be interleaved, as in the "A" and "B" example above.
56
58 $gov = CPS::Governor::Deferred->new( %args )
59 Returns a new instance of a "CPS::Governor::Deferred" object. Requires
60 no parameters but may take any of the following to adjust its default
61 behaviour:
62
63 defer_after => INT
64 If given some positive number, $n then the first "$n-1"
65 invocations of the "again" method will in fact be executed
66 immediately. Thereafter they will be enqueued in the normal
67 mechanism. This gives the effect that longrunning loops will be
68 executed in batches of $n.
69
70 If not supplied then every invocation of "again" will use the
71 queueing mechanism.
72
74 $pending = $gov->is_pending
75 Returns true if at least one code reference has been stored that hasn't
76 yet been invoked.
77
78 $gov->prod
79 Invokes all of the currently-stored code references, in the order they
80 were stored. If any new references are stored by these, they will not
81 yet be invoked, but will be available for the next time this method is
82 called.
83
84 $gov->flush
85 Repeatedly calls "prod" until no more code references are pending.
86
88 The following methods are used internally to implement the
89 functionality, which may be useful to implementors of subclasses.
90
91 $gov->later( $code, @args )
92 Used to enqueue the $code ref to be invoked later with the given @args,
93 once it is determined this should be deferred (rather than being
94 invoked immediately in the case of the first few invocations when
95 "defer_after" is set).
96
98 Paul Evans <leonerd@leonerd.org.uk>
99
100
101
102perl v5.28.0 2018-07-14 CPS::Governor::Deferred(3)