Back to index

python3.2  3.2.2
Classes | Functions | Variables
concurrent.futures._base Namespace Reference

Classes

class  Error
class  CancelledError
class  TimeoutError
class  _Waiter
class  _AsCompletedWaiter
class  _FirstCompletedWaiter
class  _AllCompletedWaiter
class  _AcquireFutures
class  Future
class  Executor

Functions

def _create_and_install_waiters
def as_completed
def wait

Variables

string __author__ = 'Brian Quinlan (brian@sweetapp.com)'
string FIRST_COMPLETED = 'FIRST_COMPLETED'
string FIRST_EXCEPTION = 'FIRST_EXCEPTION'
string ALL_COMPLETED = 'ALL_COMPLETED'
string _AS_COMPLETED = '_AS_COMPLETED'
string PENDING = 'PENDING'
string RUNNING = 'RUNNING'
string CANCELLED = 'CANCELLED'
string CANCELLED_AND_NOTIFIED = 'CANCELLED_AND_NOTIFIED'
string FINISHED = 'FINISHED'
list _FUTURE_STATES
dictionary _STATE_TO_DESCRIPTION_MAP
tuple LOGGER = logging.getLogger("concurrent.futures")
tuple DoneAndNotDoneFutures

Function Documentation

def concurrent.futures._base._create_and_install_waiters (   fs,
  return_when 
) [private]

Definition at line 151 of file _base.py.

00151 
00152 def _create_and_install_waiters(fs, return_when):
00153     if return_when == _AS_COMPLETED:
00154         waiter = _AsCompletedWaiter()
00155     elif return_when == FIRST_COMPLETED:
00156         waiter = _FirstCompletedWaiter()
00157     else:
00158         pending_count = sum(
00159                 f._state not in [CANCELLED_AND_NOTIFIED, FINISHED] for f in fs)
00160 
00161         if return_when == FIRST_EXCEPTION:
00162             waiter = _AllCompletedWaiter(pending_count, stop_on_exception=True)
00163         elif return_when == ALL_COMPLETED:
00164             waiter = _AllCompletedWaiter(pending_count, stop_on_exception=False)
00165         else:
00166             raise ValueError("Invalid return condition: %r" % return_when)
00167 
00168     for f in fs:
00169         f._waiters.append(waiter)
00170 
00171     return waiter

Here is the call graph for this function:

Here is the caller graph for this function:

def concurrent.futures._base.as_completed (   fs,
  timeout = None 
)
An iterator over the given futures that yields each as it completes.

Args:
    fs: The sequence of Futures (possibly created by different Executors) to
        iterate over.
    timeout: The maximum number of seconds to wait. If None, then there
        is no limit on the wait time.

Returns:
    An iterator that yields the given Futures as they complete (finished or
    cancelled).

Raises:
    TimeoutError: If the entire result iterator could not be generated
        before the given timeout.

Definition at line 172 of file _base.py.

00172 
00173 def as_completed(fs, timeout=None):
00174     """An iterator over the given futures that yields each as it completes.
00175 
00176     Args:
00177         fs: The sequence of Futures (possibly created by different Executors) to
00178             iterate over.
00179         timeout: The maximum number of seconds to wait. If None, then there
00180             is no limit on the wait time.
00181 
00182     Returns:
00183         An iterator that yields the given Futures as they complete (finished or
00184         cancelled).
00185 
00186     Raises:
00187         TimeoutError: If the entire result iterator could not be generated
00188             before the given timeout.
00189     """
00190     if timeout is not None:
00191         end_time = timeout + time.time()
00192 
00193     with _AcquireFutures(fs):
00194         finished = set(
00195                 f for f in fs
00196                 if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
00197         pending = set(fs) - finished
00198         waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
00199 
00200     try:
00201         for future in finished:
00202             yield future
00203 
00204         while pending:
00205             if timeout is None:
00206                 wait_timeout = None
00207             else:
00208                 wait_timeout = end_time - time.time()
00209                 if wait_timeout < 0:
00210                     raise TimeoutError(
00211                             '%d (of %d) futures unfinished' % (
00212                             len(pending), len(fs)))
00213 
00214             waiter.event.wait(wait_timeout)
00215 
00216             with waiter.lock:
00217                 finished = waiter.finished_futures
00218                 waiter.finished_futures = []
00219                 waiter.event.clear()
00220 
00221             for future in finished:
00222                 yield future
00223                 pending.remove(future)
00224 
00225     finally:
00226         for f in fs:
00227             f._waiters.remove(waiter)

Here is the call graph for this function:

def concurrent.futures._base.wait (   fs,
  timeout = None,
  return_when = ALL_COMPLETED 
)
Wait for the futures in the given sequence to complete.

Args:
    fs: The sequence of Futures (possibly created by different Executors) to
        wait upon.
    timeout: The maximum number of seconds to wait. If None, then there
        is no limit on the wait time.
    return_when: Indicates when this function should return. The options
        are:

        FIRST_COMPLETED - Return when any future finishes or is
                          cancelled.
        FIRST_EXCEPTION - Return when any future finishes by raising an
                          exception. If no future raises an exception
                          then it is equivalent to ALL_COMPLETED.
        ALL_COMPLETED -   Return when all futures finish or are cancelled.

Returns:
    A named 2-tuple of sets. The first set, named 'done', contains the
    futures that completed (is finished or cancelled) before the wait
    completed. The second set, named 'not_done', contains uncompleted
    futures.

Definition at line 230 of file _base.py.

00230 
00231 def wait(fs, timeout=None, return_when=ALL_COMPLETED):
00232     """Wait for the futures in the given sequence to complete.
00233 
00234     Args:
00235         fs: The sequence of Futures (possibly created by different Executors) to
00236             wait upon.
00237         timeout: The maximum number of seconds to wait. If None, then there
00238             is no limit on the wait time.
00239         return_when: Indicates when this function should return. The options
00240             are:
00241 
00242             FIRST_COMPLETED - Return when any future finishes or is
00243                               cancelled.
00244             FIRST_EXCEPTION - Return when any future finishes by raising an
00245                               exception. If no future raises an exception
00246                               then it is equivalent to ALL_COMPLETED.
00247             ALL_COMPLETED -   Return when all futures finish or are cancelled.
00248 
00249     Returns:
00250         A named 2-tuple of sets. The first set, named 'done', contains the
00251         futures that completed (is finished or cancelled) before the wait
00252         completed. The second set, named 'not_done', contains uncompleted
00253         futures.
00254     """
00255     with _AcquireFutures(fs):
00256         done = set(f for f in fs
00257                    if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
00258         not_done = set(fs) - done
00259 
00260         if (return_when == FIRST_COMPLETED) and done:
00261             return DoneAndNotDoneFutures(done, not_done)
00262         elif (return_when == FIRST_EXCEPTION) and done:
00263             if any(f for f in done
00264                    if not f.cancelled() and f.exception() is not None):
00265                 return DoneAndNotDoneFutures(done, not_done)
00266 
00267         if len(done) == len(fs):
00268             return DoneAndNotDoneFutures(done, not_done)
00269 
00270         waiter = _create_and_install_waiters(fs, return_when)
00271 
00272     waiter.event.wait(timeout)
00273     for f in fs:
00274         f._waiters.remove(waiter)
00275 
00276     done.update(waiter.finished_futures)
00277     return DoneAndNotDoneFutures(done, set(fs) - done)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

string concurrent.futures._base.__author__ = 'Brian Quinlan (brian@sweetapp.com)'

Definition at line 4 of file _base.py.

Definition at line 15 of file _base.py.

Initial value:
00001 [
00002     PENDING,
00003     RUNNING,
00004     CANCELLED,
00005     CANCELLED_AND_NOTIFIED,
00006     FINISHED
00007 ]

Definition at line 26 of file _base.py.

Initial value:
00001 {
00002     PENDING: "pending",
00003     RUNNING: "running",
00004     CANCELLED: "cancelled",
00005     CANCELLED_AND_NOTIFIED: "cancelled",
00006     FINISHED: "finished"
00007 }

Definition at line 34 of file _base.py.

Definition at line 14 of file _base.py.

Definition at line 21 of file _base.py.

Definition at line 23 of file _base.py.

Initial value:
00001 collections.namedtuple(
00002         'DoneAndNotDoneFutures', 'done not_done')

Definition at line 228 of file _base.py.

Definition at line 24 of file _base.py.

Definition at line 12 of file _base.py.

Definition at line 13 of file _base.py.

tuple concurrent.futures._base.LOGGER = logging.getLogger("concurrent.futures")

Definition at line 43 of file _base.py.

Definition at line 18 of file _base.py.

Definition at line 19 of file _base.py.