|
| 1 | + |
| 2 | +## Examples |
| 3 | + |
| 4 | +**Run async task until cancelled** |
| 5 | + |
| 6 | +Create cancellation and then run work in a background thread until it is cancelled. |
| 7 | + |
| 8 | +```ruby |
| 9 | +cancellation, origin = Concurrent::Cancellation.new |
| 10 | +# - origin is used for cancelling, resolve it to cancel |
| 11 | +# - cancellation is passed down to tasks for cooperative cancellation |
| 12 | +async_task = Concurrent::Promises.future(cancellation) do |cancellation| |
| 13 | + # Do work repeatedly until it is cancelled |
| 14 | + do_stuff until cancellation.canceled? |
| 15 | + :stopped_gracefully |
| 16 | +end |
| 17 | + |
| 18 | +sleep 0.01 |
| 19 | +# Wait a bit then stop the thread by resolving the origin of the cancellation |
| 20 | +origin.resolve |
| 21 | +async_task.value! |
| 22 | +``` |
| 23 | + |
| 24 | +Or let it raise an error. |
| 25 | + |
| 26 | +```ruby |
| 27 | +cancellation, origin = Concurrent::Cancellation.new |
| 28 | +async_task = Concurrent::Promises.future(cancellation) do |cancellation| |
| 29 | + # Do work repeatedly until it is cancelled |
| 30 | + while true |
| 31 | + cancellation.check! |
| 32 | + do_stuff |
| 33 | + end |
| 34 | +end |
| 35 | + |
| 36 | +sleep 0.01 |
| 37 | +# Wait a bit then stop the thread by resolving the origin of the cancellation |
| 38 | +origin.resolve |
| 39 | +async_task.result |
| 40 | +``` |
| 41 | + |
| 42 | +**Run additional tasks on Cancellation** |
| 43 | + |
| 44 | +Cancellation can also be used to log or plan re-execution. |
| 45 | + |
| 46 | +```ruby |
| 47 | +cancellation.origin.chain do |
| 48 | + # This block is executed after the Cancellation is cancelled |
| 49 | + # It can then log cancellation or e.g. plan new re-execution |
| 50 | +end |
| 51 | +``` |
| 52 | + |
| 53 | +**Run only for limited time – Timeout replacement** |
| 54 | + |
| 55 | +Execute task for a given time then finish. |
| 56 | +Instead of letting Cancellation crate its own origin, it can be passed in as argument. |
| 57 | +The passed in origin is scheduled to be resolved in given time which then cancels the Cancellation. |
| 58 | + |
| 59 | +```ruby |
| 60 | +timeout = Concurrent::Cancellation.new Concurrent::Promises.schedule(0.02) |
| 61 | +# or using shortcut helper method |
| 62 | +timeout = Concurrent::Cancellation.timeout 0.02 |
| 63 | +count = Concurrent::AtomicFixnum.new |
| 64 | +Concurrent.global_io_executor.post(timeout) do |timeout| |
| 65 | + # do stuff until cancelled |
| 66 | + count.increment until timeout.canceled? |
| 67 | +end # |
| 68 | + |
| 69 | +timeout.origin.wait |
| 70 | +count.value # => 177576 |
| 71 | +``` |
| 72 | + |
| 73 | +**Parallel background processing with single cancellation** |
| 74 | + |
| 75 | +Each task tries to count to 1000 but there is a randomly failing test. The |
| 76 | +tasks share single cancellation, when one of them fails it cancels the others. |
| 77 | +The failing tasks ends with an error, the other tasks are gracefully cancelled. |
| 78 | + |
| 79 | +```ruby |
| 80 | +cancellation, origin = Concurrent::Cancellation.new |
| 81 | +tasks = 4.times.map do |i| |
| 82 | + Concurrent::Promises.future(cancellation, origin, i) do |cancellation, origin, i| |
| 83 | + count = 0 |
| 84 | + 100.times do |
| 85 | + break count = :cancelled if cancellation.canceled? |
| 86 | + count += 1 |
| 87 | + sleep 0.001 |
| 88 | + if rand > 0.95 |
| 89 | + origin.resolve # cancel |
| 90 | + raise 'random error' |
| 91 | + end |
| 92 | + count |
| 93 | + end |
| 94 | + end |
| 95 | +end |
| 96 | +Concurrent::Promises.zip(*tasks).result # |
| 97 | +# => [false, |
| 98 | +# [:cancelled, nil, :cancelled, :cancelled], |
| 99 | +# [nil, #<RuntimeError: random error>, nil, nil]] |
| 100 | +``` |
| 101 | + |
| 102 | +Without the randomly failing part it produces following. |
| 103 | + |
| 104 | +```ruby |
| 105 | +cancellation, origin = Concurrent::Cancellation.new |
| 106 | +tasks = 4.times.map do |i| |
| 107 | + Concurrent::Promises.future(cancellation, origin, i) do |cancellation, origin, i| |
| 108 | + count = 0 |
| 109 | + 100.times do |
| 110 | + break count = :cancelled if cancellation.canceled? |
| 111 | + count += 1 |
| 112 | + sleep 0.001 |
| 113 | + # if rand > 0.95 |
| 114 | + # origin.resolve |
| 115 | + # raise 'random error' |
| 116 | + # end |
| 117 | + count |
| 118 | + end |
| 119 | + end |
| 120 | +end |
| 121 | +Concurrent::Promises.zip(*tasks).result |
| 122 | +``` |
| 123 | + |
| 124 | +**Combine cancellations** |
| 125 | + |
| 126 | +The combination created by joining two cancellations cancels when the first or the other does. |
| 127 | + |
| 128 | +```ruby |
| 129 | +cancellation_a, origin_a = Concurrent::Cancellation.new |
| 130 | +cancellation_b, origin_b = Concurrent::Cancellation.new |
| 131 | +combined_cancellation = cancellation_a.join(cancellation_b) |
| 132 | + |
| 133 | +origin_a.resolve |
| 134 | + |
| 135 | +cancellation_a.canceled? |
| 136 | +cancellation_b.canceled? |
| 137 | +combined_cancellation.canceled? |
| 138 | +``` |
| 139 | + |
| 140 | +If a different rule for joining is needed, the source can be combined manually. |
| 141 | +The manually created cancellation cancels when both the first and the other cancels. |
| 142 | + |
| 143 | +```ruby |
| 144 | +cancellation_a, origin_a = Concurrent::Cancellation.new |
| 145 | +cancellation_b, origin_b = Concurrent::Cancellation.new |
| 146 | +# cancels only when both a and b is cancelled |
| 147 | +combined_cancellation = Concurrent::Cancellation.new origin_a & origin_b |
| 148 | + |
| 149 | +origin_a.resolve |
| 150 | + |
| 151 | +cancellation_a.canceled? #=> true |
| 152 | +cancellation_b.canceled? #=> false |
| 153 | +combined_cancellation.canceled? #=> false |
| 154 | + |
| 155 | +origin_b.resolve |
| 156 | +combined_cancellation.canceled? #=> true |
| 157 | +``` |
| 158 | + |
0 commit comments