App Engine Python SDK  v1.6.9 rev.445
The Python runtime is available as an experimental Preview feature.
Public Member Functions | Static Public Attributes | List of all members
google.appengine.tools.adaptive_thread_pool.ThreadGate Class Reference
Inheritance diagram for google.appengine.tools.adaptive_thread_pool.ThreadGate:

Public Member Functions

def __init__
 
def num_threads
 
def EnableThread
 
def EnableAllThreads
 
def StartWork
 
def FinishWork
 

Static Public Attributes

string INCREASE = 'increase'
 
string HOLD = 'hold'
 
string DECREASE = 'decrease'
 

Detailed Description

Manage the number of active worker threads.

The ThreadGate limits the number of threads that are simultaneously
active in order to implement adaptive rate control.

Initially the ThreadGate allows only one thread to be active.  For
each successful work item, another thread is activated and for each
failed item, the number of active threads is reduced by one.  When only
one thread is active, failures will cause exponential backoff.

For example, a ThreadGate instance, thread_gate can be used in a number
of threads as so:

# Block until this thread is enabled for work.
thread_gate.StartWork()
try:
  status = DoSomeWorkInvolvingLimitedSharedResources()
  succeeded = IsStatusGood(status)
  badly_failed = IsStatusVeryBad(status)
finally:
  if succeeded:
    # Succeeded, add more simultaneously enabled threads to the task.
    thread_gate.FinishWork(instruction=ThreadGate.INCREASE)
  elif badly_failed:
    # Failed, or succeeded but with high resource load, reduce number of
    # workers.
    thread_gate.FinishWork(instruction=ThreadGate.DECREASE)
  else:
    # We succeeded, but don't want to add more workers to the task.
    thread_gate.FinishWork(instruction=ThreadGate.HOLD)

the thread_gate will enable and disable/backoff threads in response to
resource load conditions.

StartWork can block indefinitely. FinishWork, while not
lock-free, should never block absent a demonic scheduler.

Constructor & Destructor Documentation

def google.appengine.tools.adaptive_thread_pool.ThreadGate.__init__ (   self,
  num_threads,
  sleep = InterruptibleSleep 
)
Constructor for ThreadGate instances.

Args:
  num_threads: The total number of threads using this gate.
  sleep: Used for dependency injection.

Member Function Documentation

def google.appengine.tools.adaptive_thread_pool.ThreadGate.EnableAllThreads (   self)
Enable all worker threads.
def google.appengine.tools.adaptive_thread_pool.ThreadGate.EnableThread (   self)
Enable one more worker thread.
def google.appengine.tools.adaptive_thread_pool.ThreadGate.FinishWork (   self,
  instruction = None 
)
Ends a critical section started with self.StartWork().
def google.appengine.tools.adaptive_thread_pool.ThreadGate.StartWork (   self)
Starts a critical section in which the number of workers is limited.

Starts a critical section which allows self.__enabled_count
simultaneously operating threads. The critical section is ended by
calling self.FinishWork().

The documentation for this class was generated from the following file: