| 1 |
NAME |
| 2 |
|
| 3 |
Future::Queue - a FIFO queue of values that uses Futures |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
use Future::Queue; |
| 8 |
use Future::AsyncAwait; |
| 9 |
|
| 10 |
my $queue = Future::Queue->new; |
| 11 |
|
| 12 |
async sub process_queue |
| 13 |
{ |
| 14 |
while(1) { |
| 15 |
my $thing = await $queue->shift; |
| 16 |
... |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
my $f = process_queue(); |
| 21 |
$queue->push( "a thing" ); |
| 22 |
|
| 23 |
DESCRIPTION |
| 24 |
|
| 25 |
Objects in this class provide a simple FIFO queue the stores arbitrary |
| 26 |
perl values. Values may be added into the queue using the "push" |
| 27 |
method, and retrieved from it using the "shift" method. |
| 28 |
|
| 29 |
Values may be stored within the queue object for shift to retrieve |
| 30 |
later, or if the queue is empty then the future that shift returns will |
| 31 |
be completed once an item becomes available. |
| 32 |
|
| 33 |
CONSTRUCTOR |
| 34 |
|
| 35 |
new |
| 36 |
|
| 37 |
$queue = Future::Queue->new( %params ); |
| 38 |
|
| 39 |
Returns a new Future::Queue instance. |
| 40 |
|
| 41 |
Takes the following named arguments: |
| 42 |
|
| 43 |
max_items => INT |
| 44 |
|
| 45 |
Since version 0.50. |
| 46 |
|
| 47 |
Optional. If defined, there can be at most the given number of items |
| 48 |
in the queue. Attempts to call "push" beyond that will yield a future |
| 49 |
that remains pending, until a subsequent "shift" operation makes |
| 50 |
enough space. |
| 51 |
|
| 52 |
prototype => STRING or OBJECT or CODE |
| 53 |
|
| 54 |
Since verison 0.51. |
| 55 |
|
| 56 |
Optional. If defined, gives either a class name, an object instance |
| 57 |
to clone or a code reference to invoke when a new pending Future |
| 58 |
instance is needed by the shift or push methods when they cannot |
| 59 |
complete immediately. |
| 60 |
|
| 61 |
$f = $prototype->(); # if CODE reference |
| 62 |
$f = $prototype->new; # otherwise |
| 63 |
|
| 64 |
If not provided, a default of Future will be used. |
| 65 |
|
| 66 |
push |
| 67 |
|
| 68 |
$queue->push( @items ); |
| 69 |
|
| 70 |
await $queue->push( @items ); |
| 71 |
|
| 72 |
Adds more items into the queue. If the queue was previously empty and |
| 73 |
there is at least one shift future waiting, then the next one will be |
| 74 |
completed by this method. |
| 75 |
|
| 76 |
Since version 0.50 this can take multiple items; earlier versions can |
| 77 |
only take one value at once. |
| 78 |
|
| 79 |
This method always returns a Future instance. If max_items is defined |
| 80 |
then it is possible that this future will be in a still-pending state; |
| 81 |
indicating that there was not yet space in the queue to add the items. |
| 82 |
It will become completed once enough "shift" calls have been made to |
| 83 |
make space for them. |
| 84 |
|
| 85 |
If max_items is not defined then these instances will always be |
| 86 |
immediately complete; it is safe to drop or ignore it, or call the |
| 87 |
method in void context. |
| 88 |
|
| 89 |
If the queue has been finished then more items cannot be pushed and an |
| 90 |
exception will be thrown. |
| 91 |
|
| 92 |
shift |
| 93 |
|
| 94 |
$item = await $queue->shift; |
| 95 |
|
| 96 |
Returns a Future that will yield the next item from the queue. If there |
| 97 |
is already an item then this will be taken and the returned future will |
| 98 |
be immediate. If not, then the returned future will be pending, and the |
| 99 |
next push method will complete it. |
| 100 |
|
| 101 |
If the queue has been finished then the future will yield an empty |
| 102 |
list, or undef in scalar context. |
| 103 |
|
| 104 |
If undef is a valid item in your queue, make sure to test this |
| 105 |
condition carefully. For example: |
| 106 |
|
| 107 |
while( ( my $item ) = await $queue->shift ) { |
| 108 |
... |
| 109 |
} |
| 110 |
|
| 111 |
Here, the await expression and the assignment are in list context, so |
| 112 |
the loop will continue to iterate while any value is assigned, even if |
| 113 |
that value is undef. The loop will only stop once no items are |
| 114 |
returned, indicating the end of the queue. |
| 115 |
|
| 116 |
shift_atmost |
| 117 |
|
| 118 |
@items = await $queue->shift_atmost( $count ); |
| 119 |
|
| 120 |
Since version 0.50. |
| 121 |
|
| 122 |
A bulk version of "shift" that can return multiple items at once. |
| 123 |
|
| 124 |
Returns a Future that will yield the next few items from the queue. If |
| 125 |
there is already at least one item in the queue then up to $count items |
| 126 |
will be taken, and the returned future will be immediate. If not, then |
| 127 |
the returned future will be pending and the next push method will |
| 128 |
complete it. |
| 129 |
|
| 130 |
finish |
| 131 |
|
| 132 |
$queue->finish; |
| 133 |
|
| 134 |
Since version 0.50. |
| 135 |
|
| 136 |
Marks that the queue is now finished. Once the current list of items |
| 137 |
has been exhausted, any further attempts to shift more will yield |
| 138 |
empty. |
| 139 |
|
| 140 |
AUTHOR |
| 141 |
|
| 142 |
Paul Evans <[email protected]> |
| 143 |
|