test_thread.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Lib » test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Lib » test » test_thread.py
# Very rudimentary test of thread module

# Create a bunch of threads, let each do some work, wait until all are done

from test_support import verbose
import random
import thread
import time

mutex = thread.allocate_lock()
rmutex = thread.allocate_lock() # for calls to random
running = 0
done = thread.allocate_lock()
done.acquire()

numtasks = 10

def task(ident):
  global running
  rmutex.acquire()
  delay = random.random() * numtasks
  rmutex.release()
  if verbose:
      print 'task', ident, 'will run for', round(delay, 1), 'sec'
  time.sleep(delay)
  if verbose:
      print 'task', ident, 'done'
  mutex.acquire()
  running = running - 1
  if running == 0:
    done.release()
  mutex.release()

next_ident = 0
def newtask():
  global next_ident, running
  mutex.acquire()
  next_ident = next_ident + 1
  if verbose:
      print 'creating task', next_ident
  thread.start_new_thread(task, (next_ident,))
  running = running + 1
  mutex.release()

for i in range(numtasks):
  newtask()

print 'waiting for all tasks to complete'
done.acquire()
print 'all tasks done'

class barrier:
  def __init__(self, n):
    self.n = n
    self.waiting = 0
    self.checkin  = thread.allocate_lock()
    self.checkout = thread.allocate_lock()
    self.checkout.acquire()

  def enter(self):
    checkin, checkout = self.checkin, self.checkout

    checkin.acquire()
    self.waiting = self.waiting + 1
    if self.waiting == self.n:
      self.waiting = self.n - 1
      checkout.release()
      return
    checkin.release()

    checkout.acquire()
    self.waiting = self.waiting - 1
    if self.waiting == 0:
      checkin.release()
      return
    checkout.release()

numtrips = 3
def task2(ident):
  global running
  for i in range(numtrips):
    if ident == 0:
      # give it a good chance to enter the next
      # barrier before the others are all out
      # of the current one
      delay = 0.001
    else:
      rmutex.acquire()
      delay = random.random() * numtasks
      rmutex.release()
    if verbose:
        print 'task', ident, 'will run for', round(delay, 1), 'sec'
    time.sleep(delay)
    if verbose:
        print 'task', ident, 'entering barrier', i
    bar.enter()
    if verbose:
        print 'task', ident, 'leaving barrier', i
  mutex.acquire()
  running = running - 1
  if running == 0:
    done.release()
  mutex.release()

print '\n*** Barrier Test ***'
if done.acquire(0):
  raise ValueError, "'done' should have remained acquired"
bar = barrier(numtasks)
running = numtasks
for i in range(numtasks):
  thread.start_new_thread(task2, (i,))
done.acquire()
print 'all tasks done'
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.