Back to index

moin  1.9.0~rc2
Public Member Functions | Private Member Functions | Private Attributes
MoinMoin.support.flup.server.threadpool.ThreadPool Class Reference

List of all members.

Public Member Functions

def __init__
def shutdown
def addJob

Private Member Functions

def _start_new_thread
def _worker

Private Attributes

 _minSpare
 _maxSpare
 _maxThreads
 _lock
 _workQueue
 _idleCount
 _workerCount
 _threads
 _stop

Detailed Description

Thread pool that maintains the number of idle threads between
minSpare and maxSpare inclusive. By default, there is no limit on
the number of threads that can be started, but this can be controlled
by maxThreads.

Definition at line 33 of file threadpool.py.


Constructor & Destructor Documentation

def MoinMoin.support.flup.server.threadpool.ThreadPool.__init__ (   self,
  minSpare = 1,
  maxSpare = 5,
  maxThreads = sys.maxint 
)

Definition at line 40 of file threadpool.py.

00040 
00041     def __init__(self, minSpare=1, maxSpare=5, maxThreads=sys.maxint):
00042         self._minSpare = minSpare
00043         self._maxSpare = maxSpare
00044         self._maxThreads = max(minSpare, maxThreads)
00045 
00046         self._lock = threading.Condition()
00047         self._workQueue = []
00048         self._idleCount = self._workerCount = maxSpare
00049 
00050         self._threads = []
00051         self._stop = False
00052 
00053         # Start the minimum number of worker threads.
00054         for i in range(maxSpare):
00055             self._start_new_thread()
            

Member Function Documentation

Definition at line 56 of file threadpool.py.

00056 
00057     def _start_new_thread(self):
00058         t = threading.Thread(target=self._worker)
00059         self._threads.append(t)
00060         t.setDaemon(True)
00061         t.start()
00062         return t
        

Here is the call graph for this function:

Here is the caller graph for this function:

Worker thread routine. Waits for a job, executes it, repeat.

Definition at line 106 of file threadpool.py.

00106 
00107     def _worker(self):
00108         """
00109         Worker thread routine. Waits for a job, executes it, repeat.
00110         """
00111         self._lock.acquire()
00112         try:
00113             while True:
00114                 while not self._workQueue and not self._stop:
00115                     self._lock.wait()
00116                 
00117                 if self._stop:
00118                     return
00119 
00120                 # We have a job to do...
00121                 job = self._workQueue.pop(0)
00122 
00123                 assert self._idleCount > 0
00124                 self._idleCount -= 1
00125 
00126                 self._lock.release()
00127 
00128                 try:
00129                     job.run()
00130                 except:
00131                     # FIXME: This should really be reported somewhere.
00132                     # But we can't simply report it to stderr because of fcgi
00133                     pass
00134 
00135                 self._lock.acquire()
00136 
00137                 if self._idleCount == self._maxSpare:
00138                     break # NB: lock still held
00139                 self._idleCount += 1
00140                 assert self._idleCount <= self._maxSpare
00141 
00142             # Die off...
00143             assert self._workerCount > self._maxSpare
00144             self._workerCount -= 1
00145         finally:
00146             self._lock.release()

Here is the caller graph for this function:

def MoinMoin.support.flup.server.threadpool.ThreadPool.addJob (   self,
  job,
  allowQueuing = True 
)
Adds a job to the work queue. The job object should have a run()
method. If allowQueuing is True (the default), the job will be
added to the work queue regardless if there are any idle threads
ready. (The only way for there to be no idle threads is if maxThreads
is some reasonable, finite limit.)

Otherwise, if allowQueuing is False, and there are no more idle
threads, the job will not be queued.

Returns True if the job was queued, False otherwise.

Definition at line 74 of file threadpool.py.

00074 
00075     def addJob(self, job, allowQueuing=True):
00076         """
00077         Adds a job to the work queue. The job object should have a run()
00078         method. If allowQueuing is True (the default), the job will be
00079         added to the work queue regardless if there are any idle threads
00080         ready. (The only way for there to be no idle threads is if maxThreads
00081         is some reasonable, finite limit.)
00082 
00083         Otherwise, if allowQueuing is False, and there are no more idle
00084         threads, the job will not be queued.
00085 
00086         Returns True if the job was queued, False otherwise.
00087         """
00088         self._lock.acquire()
00089         try:
00090             # Maintain minimum number of spares.
00091             while self._idleCount < self._minSpare and \
00092                   self._workerCount < self._maxThreads:
00093                 self._workerCount += 1
00094                 self._idleCount += 1
00095                 self._start_new_thread()
00096 
00097             # Hand off the job.
00098             if self._idleCount or allowQueuing:
00099                 self._workQueue.append(job)
00100                 self._lock.notify()
00101                 return True
00102             else:
00103                 return False
00104         finally:
00105             self._lock.release()

Here is the call graph for this function:

shutdown all workers.

Definition at line 63 of file threadpool.py.

00063 
00064     def shutdown(self):
00065         """shutdown all workers."""
00066         self._lock.acquire()
00067         self._stop = True
00068         self._lock.notifyAll()
00069         self._lock.release()
00070 
00071         # wait for all threads to finish
00072         for t in self._threads:
00073             t.join()

Here is the caller graph for this function:


Member Data Documentation

Definition at line 47 of file threadpool.py.

Definition at line 45 of file threadpool.py.

Definition at line 42 of file threadpool.py.

Definition at line 43 of file threadpool.py.

Definition at line 41 of file threadpool.py.

Definition at line 50 of file threadpool.py.

Definition at line 49 of file threadpool.py.

Definition at line 47 of file threadpool.py.

Definition at line 46 of file threadpool.py.


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