An Interesting Example on Demonstrating the Feature of Threading in Python
Created at 2018-08-05 Updated at 2018-08-05 Category Python
Threading in Python is fake parallelism due to GIL, and in TensorFlow, the TF code will release the GIL when runing the C++ code in parallel. The best practise of using multi-thread in Python is to do tasks with high I/O and low CPU intensity. For high I/O, here is an example.
As you can see the result, by using multi-thread, it is 2 times faster than using a single thread. How about using multi-threading to do CPU intensive computing task? Here we go.
As you can see the result, by launching 2 threads, the total time is 14.689 seconds, and each thread costs 14.670, looks they ran in parallel, didnot they? However, by checking the result of function
compute_task_launcher2() you will see that in fact when running serially, the computation task only cost 7.077 seconds, approximatly half of that in multi-threadings.
And the total time cost in serialized threading is 14.177, slightly smaller than that of multi-theading. In conclusion, nearly 3% loss of time based on your computer and OS regardless of how many threads you spwan.