Module: Concurrent::Promises
- Extended by:
- FactoryMethods
- Defined in:
- lib/concurrent/edge/promises.rb,
lib/concurrent/edge/throttle.rb,
lib/concurrent/edge/old_channel_integration.rb,
lib/concurrent/edge/promises.rb
Overview
Promises is a new framework unifying former tools Concurrent::Future
,
Concurrent::Promise
, Concurrent::IVar
, Concurrent::Event
,
Concurrent.dataflow
, Delay
, and TimerTask
of concurrent-ruby. It
extensively uses the new synchronization layer to make all the methods
lock-free (with the exception of obviously blocking operations like #wait
,
#value
, etc.). As a result it lowers danger of deadlocking and offers
better performance.
It provides similar tools as other promise libraries do, users coming from other languages and other promise libraries will find the same tools here (probably named differently though). The naming conventions were borrowed heavily from JS promises.
This framework, however, is not just a re-implementation of other promise library, it draws inspiration from many other promise libraries, adds new ideas, and is integrated with other abstractions like actors and channels.
Therefore it is likely that user will find a suitable solution for a problem in this framework. If the problem is simple user can pick one suitable abstraction, e.g. just promises or actors. If the problem is complex user can combine parts (promises, channels, actors) which were designed to work together well to a solution. Rather than having to combine fragilely independent tools.
This framework allows its users to:
- Process tasks asynchronously
- Chain, branch, and zip the asynchronous tasks together
- Therefore, to create directed acyclic graph (hereafter DAG) of tasks
- Create delayed tasks (or delayed DAG of tasks)
- Create scheduled tasks (or delayed DAG of tasks)
- Deal with errors through rejections
- Reduce danger of deadlocking
- Control the concurrency level of tasks
- Simulate thread-like processing without occupying threads
- It allows to create tens of thousands simulations on one thread pool
- It works well on all Ruby implementations
- Use actors to maintain isolated states and to seamlessly combine it with promises
- Build parallel processing stream system with back pressure (parts, which are not keeping up, signal to the other parts of the system to slow down).
The guide is best place to start with promises, see promises.out.
Main classes
The main public user-facing classes are Event and Future which share common ancestor AbstractEventFuture.
Common ancestor of Event and Future classes, many shared methods are defined here.
Represents an event which will happen in future (will be resolved). The event is either pending or resolved. It should be always resolved. Use Future to communicate rejections and cancellation.
Represents a value which will become available in future. May reject with a reason instead, e.g. when the tasks raises an exception.
Defined Under Namespace
Modules: FactoryMethods, Resolvable Classes: AbstractEventFuture, Channel, Event, Future, ResolvableEvent, ResolvableFuture
Class Method Summary (collapse)
-
+ (Future) any_event(*futures_and_or_events)
extended
from FactoryMethods
Shortcut of FactoryMethods#any_event_on with default
:io
executor supplied. -
+ (Event) any_event_on(default_executor, *futures_and_or_events)
extended
from FactoryMethods
Creates new event which becomes resolved after first of the futures_and_or_events resolves.
-
+ (Future) any_fulfilled_future(*futures_and_or_events)
extended
from FactoryMethods
Shortcut of FactoryMethods#any_fulfilled_future_on with default
:io
executor supplied. -
+ (Future) any_fulfilled_future_on(default_executor, *futures_and_or_events)
extended
from FactoryMethods
Creates new future which is resolved after first of futures_and_or_events is fulfilled.
-
+ (Future) any_resolved_future(*futures_and_or_events)
(also: #any)
extended
from FactoryMethods
Shortcut of FactoryMethods#any_resolved_future_on with default
:io
executor supplied. -
+ (Future) any_resolved_future_on(default_executor, *futures_and_or_events)
extended
from FactoryMethods
Creates new future which is resolved after first futures_and_or_events is resolved.
-
+ (Event, Future) create(argument = nil, default_executor = self.default_executor)
extended
from FactoryMethods
General constructor.
-
+ (Executor, :io, :fast) default_executor
extended
from FactoryMethods::Configuration
The executor which is used when none is supplied to a factory method.
-
+ (Future) delay(*args, &task)
extended
from FactoryMethods
Shortcut of FactoryMethods#delay_on with default
:io
executor supplied. -
+ (Future) delay_on(default_executor, *args) {|*args| ... }
extended
from FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor.
-
+ (Future) fulfilled_future(value, default_executor = self.default_executor)
extended
from FactoryMethods
Creates resolved future with will be fulfilled with the given value.
-
+ (Future) future(*args, &task)
extended
from FactoryMethods
Shortcut of FactoryMethods#future_on with default
:io
executor supplied. -
+ (Future) future_on(default_executor, *args) {|*args| ... }
extended
from FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor.
-
+ (Future) rejected_future(reason, default_executor = self.default_executor)
extended
from FactoryMethods
Creates resolved future with will be rejected with the given reason.
-
+ (ResolvableEvent) resolvable_event
extended
from FactoryMethods
Shortcut of FactoryMethods#resolvable_event_on with default
:io
executor supplied. -
+ (ResolvableEvent) resolvable_event_on(default_executor = self.default_executor)
extended
from FactoryMethods
Created resolvable event, user is responsible for resolving the event once by ResolvableEvent#resolve.
-
+ (ResolvableFuture) resolvable_future
extended
from FactoryMethods
Shortcut of FactoryMethods#resolvable_future_on with default
:io
executor supplied. -
+ (ResolvableFuture) resolvable_future_on(default_executor = self.default_executor)
extended
from FactoryMethods
Creates resolvable future, user is responsible for resolving the future once by ResolvableFuture#resolve, ResolvableFuture#fulfill, or ResolvableFuture#reject.
-
+ (Event) resolved_event(default_executor = self.default_executor)
extended
from FactoryMethods
Creates resolved event.
-
+ (Future) resolved_future(fulfilled, value, reason, default_executor = self.default_executor)
extended
from FactoryMethods
Creates resolved future with will be either fulfilled with the given value or rejection with the given reason.
-
+ (Future) schedule(intended_time, *args, &task)
extended
from FactoryMethods
Shortcut of FactoryMethods#schedule_on with default
:io
executor supplied. -
+ (Future) schedule_on(default_executor, intended_time, *args) {|*args| ... }
extended
from FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor.
-
+ (Future) select_channel(*channels)
extended
from FactoryMethods::NewChannelIntegration
Selects a channel which is ready to be read from.
-
+ (Event) zip_events(*futures_and_or_events)
extended
from FactoryMethods
Shortcut of FactoryMethods#zip_events_on with default
:io
executor supplied. -
+ (Event) zip_events_on(default_executor, *futures_and_or_events)
extended
from FactoryMethods
Creates new event which is resolved after all futures_and_or_events are resolved.
-
+ (Future) zip_futures(*futures_and_or_events)
(also: #zip)
extended
from FactoryMethods
Shortcut of FactoryMethods#zip_futures_on with default
:io
executor supplied. -
+ (Future) zip_futures_on(default_executor, *futures_and_or_events)
extended
from FactoryMethods
Creates new future which is resolved after all futures_and_or_events are resolved.
Class Method Details
+ (Future) any_event(*futures_and_or_events) Originally defined in module FactoryMethods
Shortcut of #any_event_on with default :io
executor supplied.
+ (Event) any_event_on(default_executor, *futures_and_or_events) Originally defined in module FactoryMethods
Creates new event which becomes resolved after first of the futures_and_or_events resolves. If resolved it does not propagate AbstractEventFuture#touch, leaving delayed futures un-executed if they are not required any more.
+ (Future) any_fulfilled_future(*futures_and_or_events) Originally defined in module FactoryMethods
Shortcut of #any_fulfilled_future_on with default :io
executor supplied.
+ (Future) any_fulfilled_future_on(default_executor, *futures_and_or_events) Originally defined in module FactoryMethods
Creates new future which is resolved after first of futures_and_or_events is fulfilled.
Its result equals result of the first resolved future or if all futures_and_or_events reject,
it has reason of the last resolved future.
If resolved it does not propagate AbstractEventFuture#touch, leaving delayed
futures un-executed if they are not required any more.
If event is supplied, which does not have value and can be only resolved, it's
represented as :fulfilled
with value nil
.
+ (Future) any_resolved_future(*futures_and_or_events) Also known as: any Originally defined in module FactoryMethods
Shortcut of #any_resolved_future_on with default :io
executor supplied.
+ (Future) any_resolved_future_on(default_executor, *futures_and_or_events) Originally defined in module FactoryMethods
Creates new future which is resolved after first futures_and_or_events is resolved.
Its result equals result of the first resolved future.
If resolved it does not propagate AbstractEventFuture#touch, leaving delayed
futures un-executed if they are not required any more.
If event is supplied, which does not have value and can be only resolved, it's
represented as :fulfilled
with value nil
.
- (Event) create(nil, default_executor = self.default_executor) - (Future) create(a_future, default_executor = self.default_executor) - (Event) create(an_event, default_executor = self.default_executor) - (Future) create(exception, default_executor = self.default_executor) - (Future) create(value, default_executor = self.default_executor) Originally defined in module FactoryMethods
General constructor. Behaves differently based on the argument's type. It's provided for convenience but it's better to be explicit.
+ (Executor, :io, :fast) default_executor Originally defined in module FactoryMethods::Configuration
Returns the executor which is used when none is supplied
to a factory method. The method can be overridden in the receivers of
include FactoryMethod
+ (Future) delay(*args, &task) Originally defined in module FactoryMethods
Shortcut of #delay_on with default :io
executor supplied.
+ (Future) delay_on(default_executor, *args) {|*args| ... } Originally defined in module FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor. The task will be evaluated only after the future is touched, see AbstractEventFuture#touch
+ (Future) fulfilled_future(value, default_executor = self.default_executor) Originally defined in module FactoryMethods
Creates resolved future with will be fulfilled with the given value.
+ (Future) future(*args, &task) Originally defined in module FactoryMethods
Shortcut of #future_on with default :io
executor supplied.
+ (Future) future_on(default_executor, *args) {|*args| ... } Originally defined in module FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor. Evaluation begins immediately.
+ (Future) rejected_future(reason, default_executor = self.default_executor) Originally defined in module FactoryMethods
Creates resolved future with will be rejected with the given reason.
+ (ResolvableEvent) resolvable_event Originally defined in module FactoryMethods
Shortcut of #resolvable_event_on with default :io
executor supplied.
+ (ResolvableEvent) resolvable_event_on(default_executor = self.default_executor) Originally defined in module FactoryMethods
Created resolvable event, user is responsible for resolving the event once by ResolvableEvent#resolve.
+ (ResolvableFuture) resolvable_future Originally defined in module FactoryMethods
Shortcut of #resolvable_future_on with default :io
executor supplied.
+ (ResolvableFuture) resolvable_future_on(default_executor = self.default_executor) Originally defined in module FactoryMethods
Creates resolvable future, user is responsible for resolving the future once by ResolvableFuture#resolve, ResolvableFuture#fulfill, or ResolvableFuture#reject
+ (Event) resolved_event(default_executor = self.default_executor) Originally defined in module FactoryMethods
Creates resolved event.
+ (Future) resolved_future(fulfilled, value, reason, default_executor = self.default_executor) Originally defined in module FactoryMethods
Creates resolved future with will be either fulfilled with the given value or rejection with the given reason.
+ (Future) schedule(intended_time, *args, &task) Originally defined in module FactoryMethods
Shortcut of #schedule_on with default :io
executor supplied.
+ (Future) schedule_on(default_executor, intended_time, *args) {|*args| ... } Originally defined in module FactoryMethods
Constructs new Future which will be resolved after block is evaluated on default executor. The task is planned for execution in intended_time.
+ (Future) select_channel(*channels) Originally defined in module FactoryMethods::NewChannelIntegration
Selects a channel which is ready to be read from.
+ (Event) zip_events(*futures_and_or_events) Originally defined in module FactoryMethods
Shortcut of #zip_events_on with default :io
executor supplied.
+ (Event) zip_events_on(default_executor, *futures_and_or_events) Originally defined in module FactoryMethods
Creates new event which is resolved after all futures_and_or_events are resolved. (Future is resolved when fulfilled or rejected.)
+ (Future) zip_futures(*futures_and_or_events) Also known as: zip Originally defined in module FactoryMethods
Shortcut of #zip_futures_on with default :io
executor supplied.
+ (Future) zip_futures_on(default_executor, *futures_and_or_events) Originally defined in module FactoryMethods
Creates new future which is resolved after all futures_and_or_events are resolved.
Its value is array of zipped future values. Its reason is array of reasons for rejection.
If there is an error it rejects.
If event is supplied, which does not have value and can be only resolved, it's
represented as :fulfilled
with value nil
.