Saturday, May 30, 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. Report your findings.
There are a number of elements to the threaddemo.py program. It incorporates threaded programming, including locks, semaphores and monitoring.
The following were my findings after reviewing a threaddemo.py program which
- Firstly the numtasks variable is set to 10, which gives a total of ten tasks to be run.
- The second variable of sema is set to 3, which allows three threads to run simultaneously.
- The third variable set is mutex, which creates a read lock on the thread.
- The final variable set is running, which keeps track of how many threads are running.
As the code runs, the tasks are completed, with a maximum of three running at the one time (as set in the sema variable). The running variable monitors this number of threads. When each thread completes running, the mutex variable allows the read lock to be released and for the next thread to be run. These continue to run until the total number of tasks listed in numtasks (ten in this case) have been processed, with a maximum of three running at the one time.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment