Ex-18

1. 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.

The program starts several threads at once and and only allow certain threads to be run simultaneously and the program will end once all the threads are finished running.

*** The program initiates 10 threads one by one by calling start tasks().
*** Loop and wait till all threads are done.
***”run(self)” shows each thread locks and unlocks the resources.
o Allow each threads a random delay time.
o Request a new quota for current thread. If concurrent threads exceeds limit then exit this block.
o Locks the control viable “running” exclusively, not allow to be updated by others.
o Unlocks the control viable “running”, allows the other thread to run.
o Assumes the current thread is finished after certain delay time.
o Decreases number of running threads.
o Unlocks the control viable “running”, allows the others to update it.
o Release the quota of concurrent limit.

Once run this program , the values of the output processing times will be different depend on theĀ  processing speeds of computer that the program is used.

The meaning of the functions are derived from python refferece library on 299, 2009 from http://www.python.org/doc/2.5.2/lib/module-threading.html

Advertisement

Follow

Get every new post delivered to your Inbox.