[Python-checkins] CVS: python/dist/src/Demo/threads find.py,1.5,1.6
Tim Peters
tim_one@users.sourceforge.net
Sat, 20 Jan 2001 23:06:53 -0800
Update of /cvsroot/python/python/dist/src/Demo/threads
In directory usw-pr-cvs1:/tmp/cvs-serv29989
Modified Files:
find.py
Log Message:
Whitespace normalization.
Index: find.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Demo/threads/find.py,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** find.py 1998/03/26 20:09:16 1.5
--- find.py 2001/01/21 07:06:51 1.6
***************
*** 33,97 ****
class WorkQ:
! # Invariants:
! # - busy and work are only modified when mutex is locked
! # - len(work) is the number of jobs ready to be taken
! # - busy is the number of jobs being done
! # - todo is locked iff there is no work and somebody is busy
!
! def __init__(self):
! self.mutex = thread.allocate()
! self.todo = thread.allocate()
! self.todo.acquire()
! self.work = []
! self.busy = 0
!
! def addwork(self, func, args):
! job = (func, args)
! self.mutex.acquire()
! self.work.append(job)
! self.mutex.release()
! if len(self.work) == 1:
! self.todo.release()
!
! def _getwork(self):
! self.todo.acquire()
! self.mutex.acquire()
! if self.busy == 0 and len(self.work) == 0:
! self.mutex.release()
! self.todo.release()
! return None
! job = self.work[0]
! del self.work[0]
! self.busy = self.busy + 1
! self.mutex.release()
! if len(self.work) > 0:
! self.todo.release()
! return job
!
! def _donework(self):
! self.mutex.acquire()
! self.busy = self.busy - 1
! if self.busy == 0 and len(self.work) == 0:
! self.todo.release()
! self.mutex.release()
!
! def _worker(self):
! time.sleep(0.00001) # Let other threads run
! while 1:
! job = self._getwork()
! if not job:
! break
! func, args = job
! apply(func, args)
! self._donework()
!
! def run(self, nworkers):
! if not self.work:
! return # Nothing to do
! for i in range(nworkers-1):
! thread.start_new(self._worker, ())
! self._worker()
! self.todo.acquire()
--- 33,97 ----
class WorkQ:
! # Invariants:
! # - busy and work are only modified when mutex is locked
! # - len(work) is the number of jobs ready to be taken
! # - busy is the number of jobs being done
! # - todo is locked iff there is no work and somebody is busy
!
! def __init__(self):
! self.mutex = thread.allocate()
! self.todo = thread.allocate()
! self.todo.acquire()
! self.work = []
! self.busy = 0
!
! def addwork(self, func, args):
! job = (func, args)
! self.mutex.acquire()
! self.work.append(job)
! self.mutex.release()
! if len(self.work) == 1:
! self.todo.release()
!
! def _getwork(self):
! self.todo.acquire()
! self.mutex.acquire()
! if self.busy == 0 and len(self.work) == 0:
! self.mutex.release()
! self.todo.release()
! return None
! job = self.work[0]
! del self.work[0]
! self.busy = self.busy + 1
! self.mutex.release()
! if len(self.work) > 0:
! self.todo.release()
! return job
!
! def _donework(self):
! self.mutex.acquire()
! self.busy = self.busy - 1
! if self.busy == 0 and len(self.work) == 0:
! self.todo.release()
! self.mutex.release()
!
! def _worker(self):
! time.sleep(0.00001) # Let other threads run
! while 1:
! job = self._getwork()
! if not job:
! break
! func, args = job
! apply(func, args)
! self._donework()
!
! def run(self, nworkers):
! if not self.work:
! return # Nothing to do
! for i in range(nworkers-1):
! thread.start_new(self._worker, ())
! self._worker()
! self.todo.acquire()
***************
*** 99,120 ****
def main():
! sys.argv.append("/tmp")
! nworkers = 4
! opts, args = getopt.getopt(sys.argv[1:], '-w:')
! for opt, arg in opts:
! if opt == '-w':
! nworkers = string.atoi(arg)
! if not args:
! args = [os.curdir]
!
! wq = WorkQ()
! for dir in args:
! wq.addwork(find, (dir, selector, wq))
!
! t1 = time.time()
! wq.run(nworkers)
! t2 = time.time()
! sys.stderr.write('Total time ' + `t2-t1` + ' sec.\n')
--- 99,120 ----
def main():
! sys.argv.append("/tmp")
! nworkers = 4
! opts, args = getopt.getopt(sys.argv[1:], '-w:')
! for opt, arg in opts:
! if opt == '-w':
! nworkers = string.atoi(arg)
! if not args:
! args = [os.curdir]
!
! wq = WorkQ()
! for dir in args:
! wq.addwork(find, (dir, selector, wq))
!
! t1 = time.time()
! wq.run(nworkers)
! t2 = time.time()
! sys.stderr.write('Total time ' + `t2-t1` + ' sec.\n')
***************
*** 123,128 ****
def selector(dir, name, fullname, stat):
! # Look for group or world writable files
! return (stat[ST_MODE] & 0022) != 0
--- 123,128 ----
def selector(dir, name, fullname, stat):
! # Look for group or world writable files
! return (stat[ST_MODE] & 0022) != 0
***************
*** 130,151 ****
def find(dir, pred, wq):
! try:
! names = os.listdir(dir)
! except os.error, msg:
! print `dir`, ':', msg
! return
! for name in names:
! if name not in (os.curdir, os.pardir):
! fullname = os.path.join(dir, name)
! try:
! stat = os.lstat(fullname)
! except os.error, msg:
! print `fullname`, ':', msg
! continue
! if pred(dir, name, fullname, stat):
! print fullname
! if S_ISDIR(stat[ST_MODE]):
! if not os.path.ismount(fullname):
! wq.addwork(find, (fullname, pred, wq))
--- 130,151 ----
def find(dir, pred, wq):
! try:
! names = os.listdir(dir)
! except os.error, msg:
! print `dir`, ':', msg
! return
! for name in names:
! if name not in (os.curdir, os.pardir):
! fullname = os.path.join(dir, name)
! try:
! stat = os.lstat(fullname)
! except os.error, msg:
! print `fullname`, ':', msg
! continue
! if pred(dir, name, fullname, stat):
! print fullname
! if S_ISDIR(stat[ST_MODE]):
! if not os.path.ismount(fullname):
! wq.addwork(find, (fullname, pred, wq))