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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
|
# Basics
## Factory methods
Future and Event are created indirectly with constructor methods in
FactoryMethods. They are not designed for inheritance but rather for
composition.
```ruby
Concurrent::Promises::FactoryMethods.instance_methods(false).sort
```
The module can be included or extended where needed.
```ruby
Class.new do
include Concurrent::Promises::FactoryMethods
def a_method
resolvable_event
end
end.new.a_method
mod = Module.new do
extend Concurrent::Promises::FactoryMethods
end #
mod.resolvable_event
```
The default executor can be changed by overriding `default_executor` method
inherited from `Concurrent::Promises::FactoryMethods`.
```ruby
mod = Module.new do
extend Concurrent::Promises::FactoryMethods
def self.default_executor
:fast
end
end #
mod.future { 1 }.default_executor
Concurrent::Promises.future { 1 }.default_executor
```
The module is already extended into {Concurrent::Promises} for convenience.
```ruby
Concurrent::Promises.resolvable_event
```
## Asynchronous task
The most basic use-case of the framework is asynchronous processing. A task can
be processed asynchronously by using a `future` factory method. The block will
be executed on an internal thread pool.
Arguments of `future` are passed to the block and evaluation starts immediately.
```ruby
future = Concurrent::Promises.future(0.1) do |duration|
sleep duration
:result
end
future.value
```
Asks if the future is resolved, here it will be still in the middle of the
sleep call.
```ruby
future.resolved?
```
Retrieving the value will block until the future is **resolved**.
```ruby
future.value
future.resolved?
```
If the task fails, we talk about the future being **rejected**.
```ruby
future = Concurrent::Promises.future { sleep 0.01; raise 'Boom' }
```
There is no result, the future was rejected with a reason.
```ruby
future.value
future.reason
```
It can be forced to raise the reason for rejection when retrieving the value.
```ruby
begin
future.value!
rescue => e
e
end
```
Which is the same as `future.value! rescue $!` which will be used hereafter.
Or it can be used directly as argument for raise, since it implements exception
method.
```ruby
raise future rescue $!
```
## States
Let's define an inspection helper for methods.
```ruby
def inspect_methods(*methods, of:)
methods.reduce({}) { |h, m| h.update m => of.send(m) }
end #
```
Event has a `pending` and a `resolved` state.
```ruby
event = Concurrent::Promises.resolvable_event #
inspect_methods(:state, :pending?, :resolved?, of: event)
event.resolve #
inspect_methods(:state, :pending?, :resolved?, of: event)
```
Future's `resolved` state is further specified to be `fulfilled` or `rejected`.
```ruby
future = Concurrent::Promises.resolvable_future #
inspect_methods(:state, :pending?, :resolved?, :fulfilled?, :rejected?,
of: future)
future.fulfill :value #
inspect_methods(:state, :pending?, :resolved?, :fulfilled?, :rejected?,
:result, :value, :reason, of: future)
future = Concurrent::Promises.rejected_future StandardError.new #
inspect_methods(:state, :pending?, :resolved?, :fulfilled?, :rejected?,
:result, :value, :reason, of: future)
```
## Direct creation of resolved futures
When an existing value has to be wrapped in a future it does not have to go
through evaluation as follows.
```ruby
Concurrent::Promises.future { sleep 0.01; :value }
```
Instead, it can be created directly as already-resolved:
```ruby
Concurrent::Promises.fulfilled_future(:value)
Concurrent::Promises.rejected_future(StandardError.new('Ups'))
Concurrent::Promises.resolved_future(true, :value, nil)
Concurrent::Promises.resolved_future(false, nil, StandardError.new('Ups'))
```
## Chaining
A big advantage of promises is the ability to chain tasks together without blocking
the current thread.
```ruby
Concurrent::Promises.
future(2) { |v| v.succ }.
then(&:succ).
value!
```
As `future` factory method takes an argument, so does the `then` method. Any
supplied arguments are passed to the block, and the library ensures that they
are visible to the block.
```ruby
Concurrent::Promises.
future('3') { |s| s.to_i }.
then(2) { |v, arg| v + arg }.
value
Concurrent::Promises.
fulfilled_future('3').
then(&:to_i).
then(2, &:+).
value
Concurrent::Promises.
fulfilled_future(1).
chain(2) { |fulfilled, value, reason, arg| value + arg }.
value
```
Passing the arguments in (similarly as for a thread `Thread.new(arg) { |arg|
do_stuff arg }`) is **required**. Both of the following bad examples may break:
```ruby
arg = 1
Thread.new { do_stuff arg }
Concurrent::Promises.future { do_stuff arg }
```
Correct:
```ruby
arg = 1
Thread.new(arg) { |arg| do_stuff arg }
Concurrent::Promises.future(arg) { |arg| do_stuff arg }
```
## Branching, and zipping
Besides chaining it can also be branched.
```ruby
head = Concurrent::Promises.fulfilled_future -1 #
branch1 = head.then(&:abs) #
branch2 = head.then(&:succ).then(&:succ) #
branch1.value!
branch2.value!
```
It can be combined back to one future by zipping (`zip`, `&`).
```ruby
branch1.zip(branch2).value!
(branch1 & branch2).
then { |a, b| a + b }.
value!
(branch1 & branch2).
then(&:+).
value!
Concurrent::Promises.
zip(branch1, branch2, branch1).
then { |*values| values.reduce(&:+) }.
value!
```
Instead of zipping only the first one can be taken, if needed.
```ruby
Concurrent::Promises.any(branch1, branch2).value!
(branch1 | branch2).value!
```
## Blocking methods
In these examples we have used blocking methods like `value` extensively for
their convenience, however in practice is better to avoid them and continue
chaining.
If they need to be used (e.g. when integrating with threads), `value!` is a
better option over `value` when rejections are not dealt with differently.
Otherwise the rejections are not handled and probably silently forgotten.
## Error handling
When a task in the chain fails, the rejection propagates down the
chain without executing the tasks created with `then`.
```ruby
Concurrent::Promises.
fulfilled_future(Object.new).
then(&:succ).
then(&:succ).
result
```
As `then` chained tasks execute only on fulfilled futures, there is a `rescue`
method which chains a task which is executed only when the future is rejected.
It can be used to recover from rejection.
Using rescue to fulfill to 0 instead of the error.
```ruby
Concurrent::Promises.
fulfilled_future(Object.new).
then(&:succ).
then(&:succ).
rescue { |err| 0 }.
result
```
Rescue not executed when there is no rejection.
```ruby
Concurrent::Promises.
fulfilled_future(1).
then(&:succ).
then(&:succ).
rescue { |e| 0 }.
result
```
Tasks added with `chain` are always evaluated.
```ruby
Concurrent::Promises.
fulfilled_future(1).
chain { |fulfilled, value, reason| fulfilled ? value : reason }.
value!
Concurrent::Promises.
rejected_future(StandardError.new('Ups')).
chain { |fulfilled, value, reason| fulfilled ? value : reason }.
value!
```
Zip is rejected if any of the zipped futures is.
```ruby
rejected_zip = Concurrent::Promises.zip(
Concurrent::Promises.fulfilled_future(1),
Concurrent::Promises.rejected_future(StandardError.new('Ups')))
rejected_zip.result
rejected_zip.
rescue { |reason1, reason2| (reason1 || reason2).message }.
value
```
## Delayed futures
Delayed futures will not evaluate until asked by `touch` or other method
requiring resolution.
```ruby
future = Concurrent::Promises.delay { sleep 0.01; 'lazy' }
sleep 0.01 #
future.resolved?
future.touch
sleep 0.02 #
future.resolved?
```
All blocking methods like `wait`, `value` call `touch` and trigger evaluation.
```ruby
Concurrent::Promises.delay { :value }.value
```
It propagates up through the chain, allowing whole or partial lazy chains.
```ruby
head = Concurrent::Promises.delay { 1 } #
branch1 = head.then(&:succ) #
branch2 = head.delay.then(&:succ) #
join = branch1 & branch2 #
sleep 0.01 #
```
Nothing resolves.
```ruby
[head, branch1, branch2, join].map(&:resolved?)
```
Force `branch1` evaluation.
```ruby
branch1.value
sleep 0.01 #
[head, branch1, branch2, join].map(&:resolved?)
```
Force evaluation of both by calling `value` on `join`.
```ruby
join.value
[head, branch1, branch2, join].map(&:resolved?)
```
## Flatting
Sometimes it is needed to wait for an inner future. An apparent solution is to wait
inside the future `Concurrent::Promises.future { Concurrent::Promises.future { 1+1 }.value }.value`.
However, as mentioned before, `value` calls should be **avoided** to avoid
blocking threads. Therefore there is a `#flat` method which is a correct solution
in this situation and does not block any thread.
```ruby
Concurrent::Promises.future { Concurrent::Promises.future { 1+1 } }.flat.value!
```
A more complicated example.
```ruby
Concurrent::Promises.
future { Concurrent::Promises.future { Concurrent::Promises.future { 1 + 1 } } }.
flat(1).
then { |future| future.then(&:succ) }.
flat(1).
value!
```
## Scheduling
Tasks can be planned to be executed with a time delay.
Schedule task to be executed in 0.1 seconds.
```ruby
scheduled = Concurrent::Promises.schedule(0.1) { 1 }
scheduled.resolved?
```
Value will become available after 0.1 seconds.
```ruby
scheduled.value
```
It can be used in the chain as well, where the delay is counted from the moment
its parent resolves. Therefore, the following future will be resolved in 0.2 seconds.
```ruby
future = Concurrent::Promises.
future { sleep 0.01; :result }.
schedule(0.01).
then(&:to_s).
value!
```
Time can be used as well.
```ruby
Concurrent::Promises.schedule(Time.now + 10) { :val }
```
## Resolvable Future and Event:
Sometimes it is required to resolve a future externally, in these cases
`resolvable_future` and `resolvable_event` factory methods can be used. See
{Concurrent::Promises::ResolvableFuture} and
{Concurrent::Promises::ResolvableEvent}.
```ruby
future = Concurrent::Promises.resolvable_future
```
The thread will be blocked until the future is resolved
```ruby
thread = Thread.new { future.value } #
future.fulfill 1
thread.value
```
A future can be resolved only once.
```ruby
future.fulfill 1 rescue $!
future.fulfill 2, false
```
## How are promises executed?
Promises use global pools to execute the tasks. Therefore each task may run on
different threads which implies that users have to be careful not to depend on
Thread-local variables (or they have to be set at the beginning of the task and
cleaned up at the end of the task).
Since the tasks are running on may different threads of the thread pool, it's
better to follow following rules:
- Use only data passed via arguments or values of parent futures, to
have better control over what are futures accessing.
- The data passed in and out of futures is easier to deal with if it is
immutable or at least treated as such.
- Any mutable and mutated object accessed by more than one thread or future
must be thread-safe, see {Concurrent::Array}, {Concurrent::Hash}, and
{Concurrent::Map}. (The value of a future may be consumed by many futures.)
- Futures can access outside objects, but they have to be thread-safe.
> *TODO: This part to be extended*
# Advanced
## Callbacks
```ruby
queue = Queue.new
future = Concurrent::Promises.delay { 1 + 1 }
future.on_fulfillment { queue << 1 } # evaluated asynchronously
future.on_fulfillment! { queue << 2 } # evaluated on resolving thread
queue.empty?
future.value
queue.pop
queue.pop
```
## Using executors
Factory methods, chain, and callback methods all have other versions of them
which takes an executor argument.
It takes an instance of an executor, or a symbol which is a shortcut for the
two global pools in concurrent-ruby. `:fast` for short and non-blocking tasks
and `:io` for long-running and blocking tasks.
```ruby
Concurrent::Promises.future_on(:fast) { 2 }.
then_on(:io) { File.read __FILE__ }.
value.size
```
## Run (simulated process)
Similar to flatting is running. When `run` is called on a future it will flat
indefinitely as long the future fulfils into a `Future` value. It can be used
to simulate a thread-like processing without actually occupying the thread.
```ruby
count = lambda do |v|
v += 1
v < 5 ? Concurrent::Promises.future_on(:fast, v, &count) : v
end
400.times.
map { Concurrent::Promises.future_on(:fast, 0, &count).run.value! }.
all? { |v| v == 5 }
```
Therefore the above example finished fine on the the `:fast` thread pool even
though it has much fewer threads than are simulated in the simulated process.
# Interoperability
## Actors
Create an actor which takes received numbers and returns the number squared.
```ruby
actor = Concurrent::Actor::Utils::AdHoc.spawn :square do
-> v { v ** 2 }
end
```
Send result of `1+1` to the actor, and add 2 to the result sent back from the
actor.
```ruby
Concurrent::Promises.
future { 1 + 1 }.
then_ask(actor).
then { |v| v + 2 }.
value!
```
So `(1 + 1)**2 + 2 = 6`.
The `ask` method returns future.
```ruby
actor.ask(2).then(&:succ).value!
```
## Channel
There is an implementation of channel as well. Let's start by creating a
channel with a capacity of 2 messages.
```ruby
ch1 = Concurrent::Promises::Channel.new 2
```
We push 3 messages, it can be observed that the last future representing the
push is not fulfilled since the capacity prevents it. When the work which fills
the channel depends on the futures created by push it can be used to create
backpressure – the filling work is delayed until the channel has space for
more messages.
```ruby
pushes = 3.times.map { |i| ch1.push_op i }
ch1.pop_op.value!
pushes
```
A selection over channels can be created with the `.select_channel` factory method. It
will be fulfilled with a first message available in any of the channels. It
returns a pair to be able to find out which channel had the message available.
```ruby
ch2 = Concurrent::Promises::Channel.new 2
result = Concurrent::Promises::Channel.select_op([ch1, ch2])
result.value!
Concurrent::Promises.future { 1+1 }.then_channel_push(ch1)
result = (
Concurrent::Promises.fulfilled_future('%02d') &
Concurrent::Promises::Channel.select_op([ch1, ch2])).
then { |format, (channel, value)| format format, value } #
result.value!
```
## ProcessingActor
There is also a new implementation of actors based on the Channel and the
ability of promises to simulate processes. The actor runs as a process but also
does not occupy a thread per actor as the previously-described Concurrent::Actor
implementation. This implementation is close to Erlang actors, therefore OTP
can be ported for this actors (and it's planned).
The simplest actor is one which just computes without even receiving a
message.
```ruby
actor = Concurrent::ProcessingActor.act(an_argument = 2) do |actor, number|
number ** 3
end
actor.termination.value!
```
Let's receive some messages though.
```ruby
add_2_messages = Concurrent::ProcessingActor.act do |actor|
# Receive two messages then terminate normally with the sum.
(actor.receive & actor.receive).then do |a, b|
a + b
end
end
add_2_messages.tell_op 1
add_2_messages.termination.resolved?
add_2_messages.tell_op 3
add_2_messages.termination.value!
```
Actors can also be used to apply backpressure to a producer. Let's start by
defining an actor which a mailbox of size 2.
```ruby
slow_counter = -> (actor, count) do
actor.receive.then do |command, number|
sleep 0.01
case command
when :add
slow_counter.call actor, count + number
when :done
# terminate
count
end
end
end
actor = Concurrent::ProcessingActor.act_listening(
Concurrent::Promises::Channel.new(2),
0,
&slow_counter)
```
Now we can create a producer which will push messages only when there is a
space available in the mailbox. We use promises to free a thread during waiting
on a free space in the mailbox.
```ruby
produce = -> receiver, i do
if i < 10
receiver.
# send a message to the actor, resolves only after the message is
# accepted by the actor's mailbox
tell_op([:add, i]).
# send incremented message when the above message is accepted
then(i+1, &produce)
else
receiver.tell_op(:done)
# do not continue
end
end
Concurrent::Promises.future(actor, 0, &produce).run.wait!
actor.termination.value!
```
# Use-cases
## Simple background processing
```ruby
Concurrent::Promises.future { do_stuff }
```
## Parallel background processing
```ruby
tasks = 4.times.map { |i| Concurrent::Promises.future(i) { |i| i*2 } }
Concurrent::Promises.zip(*tasks).value!
```
## Actor background processing
Actors are mainly keep and isolate state, they should stay responsive not being
blocked by a longer running computations. It desirable to offload the work to
stateless promises.
Lets define an actor which will process jobs, while staying responsive, and
tracking the number of tasks being processed.
```ruby
class Computer < Concurrent::Actor::RestartingContext
def initialize
super()
@jobs = {}
end
def on_message(msg)
command, *args = msg
case command
# new job to process
when :run
job = args[0]
@jobs[job] = envelope.future
# Process asynchronously and send message back when done.
Concurrent::Promises.future(&job).chain(job) do |fulfilled, value, reason, job|
self.tell [:done, job, fulfilled, value, reason]
end
# Do not make return value of this method to be answer of this message.
# We are answering later in :done by resolving the future kept in @jobs.
Concurrent::Actor::Behaviour::MESSAGE_PROCESSED
when :done
job, fulfilled, value, reason = *args
future = @jobs.delete job
# Answer the job's result.
future.resolve fulfilled, value, reason
when :status
{ running_jobs: @jobs.size }
else
# Continue to fail with unknown message.
pass
end
end
end
```
Create the computer actor and send it 3 jobs.
```ruby
computer = Concurrent::Actor.spawn Computer, :computer
results = 3.times.map { computer.ask [:run, -> { sleep 0.01; :result }] }
computer.ask(:status).value!
results.map(&:value!)
```
## Solving the Thread count limit by thread simulation
Sometimes an application requires to process a lot of tasks concurrently. If
the number of concurrent tasks is high enough than it is not possible to create
a Thread for each of them. A partially satisfactory solution could be to use
Fibers, but that solution locks the application on MRI since other Ruby
implementations are using threads for each Fiber.
This library provides a {Concurrent::Promises::Future#run} method on a future
to simulate threads without actually accepting one all the time. The run method
is similar to {Concurrent::Promises::Future#flat} but it will keep flattening
until it's fulfilled with non future value, then the value is taken as a result
of the process simulated by `run`.
```ruby
body = lambda do |v|
# Some computation step of the process
new_v = v + 1
# Is the process finished?
if new_v < 5
# Continue computing with new value, does not have to be recursive.
# It just has to return a future.
Concurrent::Promises.future(new_v, &body)
else
# The process is finished, fulfill the final value with `new_v`.
new_v
end
end
Concurrent::Promises.future(0, &body).run.value! # => 5
```
This solution works well an any Ruby implementation.
> *TODO: More examples to be added.*
## Throttling concurrency
By creating an actor managing the resource we can control how many threads is
accessing the resource. In this case one at the time.
```ruby
data = Array.new(10) { |i| '*' * i }
DB = Concurrent::Actor::Utils::AdHoc.spawn :db, data do |data|
lambda do |message|
# pretending that this queries a DB
data[message]
end
end
concurrent_jobs = 11.times.map do |v|
DB.
# ask the DB with the `v`, only one at the time, rest is parallel
ask(v).
# get size of the string, rejects for 11
then(&:size).
# translate error to a value (message of the exception)
rescue { |reason| reason.message }
end #
Concurrent::Promises.zip(*concurrent_jobs).value!
```
Often there is more then one DB connections, then the pool can be used.
```ruby
pool_size = 5
DB_POOL = Concurrent::Actor::Utils::Pool.spawn!('DB-pool', pool_size) do |index|
# DB connection constructor
Concurrent::Actor::Utils::AdHoc.spawn(
name: "connection-#{index}",
args: [data]) do |data|
lambda do |message|
# pretending that this queries a DB
data[message]
end
end
end
concurrent_jobs = 11.times.map do |v|
DB_POOL.
# ask the DB with the `v`, only one at the time, rest is parallel
ask(v).
# get size of the string, rejects for 11
then(&:size).
# translate error to a value (message of the exception)
rescue { |reason| reason.message }
end #
Concurrent::Promises.zip(*concurrent_jobs).value!
```
In other cases the DB adapter maintains its internal connection pool and we
just need to limit concurrent access to the DB's API to avoid the calls being
blocked.
Lets pretend that the `#[]` method on `DB_INTERNAL_POOL` is using the internal
pool of size 3. We create throttle with the same size
```ruby
DB_INTERNAL_POOL = Concurrent::Array.new data
max_tree = Concurrent::Throttle.new 3
futures = 11.times.map do |i|
max_tree.
# throttled tasks, at most 3 simultaneous calls of [] on the database
future { DB_INTERNAL_POOL[i] }.
# un-throttled tasks, unlimited concurrency
then { |starts| starts.size }.
rescue { |reason| reason.message }
end #
futures.map(&:value!)
```
## Long stream of tasks, applying backpressure
Let's assume that we are querying an API for data and the queries can be faster
than we are able to process them. This example shows how to use channel as a
buffer and how to apply backpressure to slow down the queries.
```ruby
require 'json' #
channel = Concurrent::Promises::Channel.new 6
cancellation, origin = Concurrent::Cancellation.new
def query_random_text(cancellation, channel)
Concurrent::Promises.future do
# for simplicity the query is omitted
# url = 'some api'
# Net::HTTP.get(URI(url))
sleep 0.01
{ 'message' =>
'Lorem ipsum rhoncus scelerisque vulputate diam inceptos'
}.to_json
end.then_flat_event(cancellation) do |value, cancellation|
# The push to channel is fulfilled only after the message is successfully
# published to the channel, therefore it will not continue querying until
# current message is pushed.
cancellation.origin | channel.push_op(value)
# It could wait on the push indefinitely if the token is not checked
# here with `or` (the pipe).
end.then(cancellation) do |cancellation|
# query again after the message is pushed to buffer
query_random_text(cancellation, channel) unless cancellation.canceled?
end
end
words = []
words_throttle = Concurrent::Throttle.new 1
def count_words_in_random_text(cancellation, channel, words, words_throttle)
channel.pop_op.then do |response|
string = JSON.load(response)['message']
# processing is slower than querying
sleep 0.02
words_count = string.scan(/\w+/).size
end.then_on(words_throttle.on(:io), words) do |words_count, words|
# safe since throttled to only 1 task at a time
words << words_count
end.then_on(:io, cancellation) do |_, cancellation|
# count words in next message
unless cancellation.canceled?
count_words_in_random_text(cancellation, channel, words, words_throttle)
end
end
end
query_processes = 3.times.map do
Concurrent::Promises.future(cancellation, channel, &method(:query_random_text)).run
end
word_counter_processes = 2.times.map do
Concurrent::Promises.future(cancellation, channel, words, words_throttle,
&method(:count_words_in_random_text)).run
end
sleep 0.05 #
```
Let it run for a while, then cancel it, and ensure that the runs were all fulfilled
(therefore ended) after the cancellation. Finally, print the result.
```ruby
origin.resolve
query_processes.map(&:wait!)
word_counter_processes.map(&:wait!)
words
```
Compared to using threads directly, this is highly configurable and composable
solution.
## Periodic task
A periodically executed task can be creating by combining `schedule`, `run` and `Cancellation`.
```ruby
repeating_scheduled_task = -> interval, cancellation, task do
Concurrent::Promises.
# Schedule the task.
schedule(interval, cancellation, &task).
# If successful schedule again.
# Alternatively use chain to schedule always.
then { repeating_scheduled_task.call(interval, cancellation, task) }
end
cancellation, origin = Concurrent::Cancellation.new
task = -> cancellation do
5.times do
cancellation.check!
do_stuff
end
end
result = Concurrent::Promises.future(0.1, cancellation, task, &repeating_scheduled_task).run
sleep 0.03 #
origin.resolve
result.result
```
|