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 Lowa. The code and sample output below are worth a look. Report your findings.
Answer:
I have copied the provided code and run the code on my laptop computer. The returned result is similar to the given result. The given result and my returned result are not the same exactly because the speed of the computers running the same code is different.
The code of the threaddemo.py simulates the lock and semaphore processing. threaddemo.py create 3 important variables. They are sema, mutex and running when numtasks limits the number of thread/task at 10. Variable (object) sema take 3 tasks to run at a time. When running the 3 tasks, Variable (object) mutex put a Read lock to the process of threading. After a wait, some tasks of the three tasks are completed, variable running keeps track on the completion of the tasks or threads. If some tasks of the three tasks are completed, variable (object) mutex release the read lock and new tasks of the remaining 7 are added and processed. Consequentially, the 10 tasks (threads) are done totally when 3 tasks are proceessed each time.
The threaddemo.py exactly simulates semaphore and locking.
29 Apr 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment