Friday, June 26, 2009

Exercise 18: Threading demonstration in Python

A simple demonstration of the threading module in Python that uses both a lock and semaphore to control concurrency is by Ted Herman at the University of Iowa. The code and sample output below are worth a look.

threaddemo.py
# Create a bunch of threads, let each do some work, wait until all are done
import random
import threading
import time

# This takes about n/3 seconds to run (about n/3 clumps of tasks, times
# about 1 second per clump).
numtasks = 10

# no more than 3 of the 10 can run at once
# create a semaphore bounded up to 3
sema = threading.BoundedSemaphore(value=3)

# create a Read Lock
mutex = threading.RLock()

# running is a global variable to keep track
# of how many threads are running
running = 0

# the TestThread class is a subclass of threading.Thread,
# so it should supply the standard methods: run, ...
class TestThread(threading.Thread):
def run(self):

# tell python we access the global variable
global running

# introduce a random delay between 0 and 2
delay = random.random() * 2
print 'task', self.getName(), 'will run for', delay, 'sec'

# first, wait on the semaphore (limited to three threads)
sema.acquire()

# but only one of these three at a time should update
# the running variable
mutex.acquire()
running = running + 1
print running, 'tasks are running'

# release lock so another can update "running"
mutex.release()

# now sleep for a while (yawn....zzzzzzz)
time.sleep(delay)

# after wakeup, say we are done
print 'task', self.getName(), 'done'

# time to decrement "running"
mutex.acquire()
running = running - 1
print self.getName(), 'is finished.', running, 'tasks are running'
mutex.release()

# and finally, exit the group of three tasks
sema.release()

# main program: build and start all the threads
threads = []

# done in a function just for convenience
def starttasks():
for i in range(numtasks):

# show off Python's formatting feature
# by building a name for each thread
t = TestThread(name=""%i)

# add new name to list
threads.append(t)

# start thread
t.start()
starttasks()
print 'waiting for all tasks to complete'

# next statement waits for all threads to finish
for t in threads: t.join()
print 'all tasks done'

Here is the output window when you run the threaddemo.py script:
PythonWin 2.3.2 (#49, Nov 13 2003, 10:34:54) [MSC v.1200 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> task <thread 0> will run for 0.120358615571 sec
1 tasks are running
task <thread 1> will run for 0.763990116379 sec
2 tasks are running
task <thread 2> will run for 0.207353153515 sec
3 tasks are running
task <thread 3> will run for 1.55806365714 sec
task <thread 4> will run for 0.776083733579 sec
task <thread 5> will run for 0.336440216469 sec
task <thread 6> will run for 1.55779500185 sec
task <thread 7> will run for 1.96896800957 sec
task <thread 8> will run for 1.57596561512 sec
task <thread 9> will run for 0.634052702735 sec
waiting for all tasks to complete
task <thread 0> done
<thread 0> is finished. 2 tasks are running
3 tasks are running
task <thread 2> done
<thread 2> is finished. 2 tasks are running
3 tasks are running
task <thread 1> done
<thread 1> is finished. 2 tasks are running
3 tasks are running
task <thread 4> done
<thread 4> is finished. 2 tasks are running
3 tasks are running
task <thread 5> done
<thread 5> is finished. 2 tasks are running
3 tasks are running
task <thread 3> done
<thread 3> is finished. 2 tasks are running
3 tasks are running
task <thread 6> done
<thread 6> is finished. 2 tasks are running
3 tasks are running
task <thread 7> done
<thread 7> is finished. 2 tasks are running
task <thread 9> done
<thread 9> is finished. 1 tasks are running
task <thread 8> done
<thread 8> is finished. 0 tasks are running
all tasks done

I have had a look at the code and the output above. I have also studied threading using Visual Studio.net in previous CSU subjects. There is a good example of threading for vb.net at http://www.startvbdotnet.com/threading/default.aspx

No comments:

Post a Comment