Exercise 18

By rico61

Exercise 18 Threading demonstration in Python

 

I created the file threaddemo.py file by copy and paste from the study guide.  The codes are paste into the shell of python and run the module.  At first I got some errors relating to the indent of my codes.  After indenting the codes into the right indent position, there were no more errors and the module was run successfully with the following output:-

task <thread 0> will run for 0.107793996677 sec

1 tasks are running

task <thread 1> will run for 1.78487913711 sec

2 tasks are running

task <thread 2> will run for 0.0919440657057 sec

3 tasks are running

task <thread 0> done

<thread 0> is finished. 2 tasks are running

task <thread 3> will run for 1.0885244399 sec

3 tasks are running

task <thread 2> done

<thread 2> is finished. 2 tasks are running

task <thread 4> will run for 0.435078238863 sec

3 tasks are running

task <thread 5> will run for 0.78092471387 sec

task <thread 6> will run for 0.144063690585 sec

task <thread 7> will run for 1.56290882456 sec

task <thread 8> will run for 1.46552151389 sec

task <thread 9> will run for 0.80860973102 sec

waiting for all tasks to complete

task <thread 4> done

<thread 4> 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 5> done

<thread 5> 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 9> done

<thread 9> is finished. 2 tasks are running

task task<thread 7>  <thread 8>done

<thread 7> done

 is finished. 1 tasks are running

<thread 8> is finished. 0 tasks are running

all tasks done

 

The format of the output is similar to the output given in the study guide.  However the values of the processing times are different due to the difference in processing speed of my desktop computer which is expected.

 

 

Leave a Reply