123456789_123456789_123456789_123456789_123456789_

Module: Concurrent::Promises

Overview

Promises is a new framework unifying former tools Future, Promise, IVar, Event, 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.

=== Main classes

The main public user-facing classes are Event and Future which share common ancestor AbstractEventFuture.

AbstractEventFuture:

Common ancestor of Event and Future classes, many shared methods are defined here.

Event:

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.

Future:

Represents a value which will become available in future. May reject with a reason instead, e.g. when the tasks raises an exception.

Class Method Summary

FactoryMethods - Extended

any
any_event

Shortcut of #any_event_on with default :io executor supplied.

any_event_on

Creates a new event which becomes resolved after the first futures_and_or_events resolves.

any_fulfilled_future

Shortcut of #any_fulfilled_future_on with default :io executor supplied.

any_fulfilled_future_on

Creates a new future which is resolved after the first futures_and_or_events is fulfilled.

any_resolved_future

Shortcut of #any_resolved_future_on with default :io executor supplied.

any_resolved_future_on

Creates a new future which is resolved after the first futures_and_or_events is resolved.

delay

Shortcut of #delay_on with default :io executor supplied.

delay_on

Creates a new event or future which is resolved only after it is touched, see Concurrent::AbstractEventFuture#touch.

fulfilled_future

Creates a resolved future which will be fulfilled with the given value.

future

Shortcut of #future_on with default :io executor supplied.

future_on

Constructs a new Future which will be resolved after block is evaluated on default executor.

make_future

General constructor.

rejected_future

Creates a resolved future which will be rejected with the given reason.

resolvable_event

Shortcut of #resolvable_event_on with default :io executor supplied.

resolvable_event_on

Creates a resolvable event, user is responsible for resolving the event once by calling ResolvableEvent#resolve.

resolvable_future

Shortcut of #resolvable_future_on with default :io executor supplied.

resolvable_future_on

Creates resolvable future, user is responsible for resolving the future once by ResolvableFuture#resolve, ResolvableFuture#fulfill, or ResolvableFuture#reject

resolved_event

Creates resolved event.

resolved_future

Creates a resolved future with will be either fulfilled with the given value or rejected with the given reason.

schedule

Shortcut of #schedule_on with default :io executor supplied.

schedule_on

Creates a new event or future which is resolved in intended_time.

zip
zip_events

Shortcut of #zip_events_on with default :io executor supplied.

zip_events_on

Creates a new event which is resolved after all futures_and_or_events are resolved.

zip_futures

Shortcut of #zip_futures_on with default :io executor supplied.

zip_futures_on

Creates a new future which is resolved after all futures_and_or_events are resolved.

zip_futures_over

Shortcut of #zip_futures_over_on with default :io executor supplied.

zip_futures_over_on

Creates new future which is resolved after all the futures created by future_factory from enumerable elements are resolved.

Promises::FactoryMethods::OldChannelIntegration - Included

select

only proof of concept.

Promises::FactoryMethods::Configuration - Included