Package duplicity :: Module dup_threading
[hide private]
[frames] | no frames]

Module dup_threading

source code

Duplicity specific but otherwise generic threading interfaces and utilities.

(Not called "threading" because we do not want to conflict with the standard threading module, and absolute imports require at least python 2.5.)

Classes [hide private]
  Value
A thread-safe container of a reference to an object (but not the object itself).
Functions [hide private]
 
threading_supported()
Returns whether threading is supported on the system we are running on.
source code
 
require_threading(reason=None)
Assert that threading is required for operation to continue.
source code
 
thread_module()
Returns the thread module, or dummy_thread if threading is not supported.
source code
 
threading_module()
Returns the threading module, or dummy_thread if threading is not supported.
source code
 
with_lock(lock, fn)
Call fn with lock acquired.
source code
 
interruptably_wait(cv, waitFor)
cv - The threading.Condition instance to wait on test - Callable returning a boolean to indicate whether the criteria being waited on has been satisfied.
source code
 
async_split(fn)
Splits the act of calling the given function into one front-end part for waiting on the result, and a back-end part for performing the work in another thread.
source code
Variables [hide private]
  _threading_supported = True
Function Details [hide private]

require_threading(reason=None)

source code 

Assert that threading is required for operation to continue. Raise an appropriate exception if this is not the case.

Reason specifies an optional reason why threading is required, which will be used for error reporting in case threading is not supported.

with_lock(lock, fn)

source code 

Call fn with lock acquired. Guarantee that lock is released upon the return of fn.

Returns the value returned by fn, or raises the exception raised by fn.

(Lock can actually be anything responding to acquire() and release().)

interruptably_wait(cv, waitFor)

source code 

cv   - The threading.Condition instance to wait on
test - Callable returning a boolean to indicate whether
       the criteria being waited on has been satisfied.

Perform a wait on a condition such that it is keyboard
interruptable when done in the main thread. Due to Python
limitations as of <= 2.5, lock acquisition and conditions waits
are not interruptable when performed in the main thread.

Currently, this comes at a cost additional CPU use, compared to a
normal wait. Future implementations may be more efficient if the
underlying python supports it.

The condition must be acquired.

This function should only be used on conditions that are never
expected to be acquired for extended periods of time, or the
lock-acquire of the underlying condition could cause an
uninterruptable state despite the efforts of this function.

There is no equivalent for acquireing a lock, as that cannot be
done efficiently.

Example:

Instead of:

  cv.acquire()
  while not thing_done:
    cv.wait(someTimeout)
  cv.release()

do:

  cv.acquire()
  interruptable_condwait(cv, lambda: thing_done)
  cv.release()

async_split(fn)

source code 

Splits the act of calling the given function into one front-end part for waiting on the result, and a back-end part for performing the work in another thread.

Returns (waiter, caller) where waiter is a function to be called in order to wait for the results of an asynchronous invokation of fn to complete, returning fn's result or propagating it's exception.

Caller is the function to call in a background thread in order to execute fn asynchronously. Caller will return (success, waiter) where success is a boolean indicating whether the function suceeded (did NOT raise an exception), and waiter is the waiter that was originally returned by the call to async_split().