Python is an interpreted language, depending on the underlying protocol there are many versions, the most common is the C-based CPython, by default we call Python is CPython.
Python's Gil (Global Interpreter Lock):
Used to resolve data integrity and state synchronization between multithreading, so that no matter how many CPUs the thread is distributed on, the interpreter only allows 1 threads to run at the same time.
So Python is thread_safe. In fact, Python is almost only single-threaded programming.
CPU Intensive Applications: (frequent calculation)
Starting from 1 accumulate, to 100 million
1 def foo (): 2 num = 03for in Range (100000001):4 num + = i
Now do a task, complete three functions foo () run, calculate the total time, give four kinds of scenarios
1, single thread, 1,2,3,4 serial
2, multi-threading, 1,2,3,4 parallel
3, single process, 1,2,3,4 serial
4, multi-process, 1,2,3,4 parallel
Experiment to verify the best way to implement Python concurrency under CPU cheats
To test your computer's CPU:
intel_i7-4700 (4-Core 8 threads)
Programme 1;
1 if __name__=="__main__":2T_list = []3Start_time =time.time ()4 forIinchRange (5):5i = Threading. Thread (target=foo)6 t_list.append (i)7 I.start ()8 I.join ()9End_time =time.time ()Ten Print("totally time:{}". Format (End_time-start_time))
Programme 2;
1 if __name__=="__main__":2T_list = []3Start_time =time.time ()4 forIinchRange (5):5i = Threading. Thread (target=foo)6 t_list.append (i)7 I.start ()8 forIincht_list:9 I.join ()TenEnd_time =time.time () One Print("totally time:{}". Format (End_time-start_time))
Programme 3;
1 if __name__=="__main__":2P_list = []3Start_time =time.time ()4 forIinchRange (5):5i = multiprocessing. Process (target=foo)6 p_list.append (i)7 I.start ()8 I.join ()9End_time =time.time ()Ten Print("totally time:{}". Format (End_time-start_time))
Programme 4;
1 if __name__=="__main__":2P_list = []3Start_time =time.time ()4 forIinchRange (5):5i = multiprocessing. Process (target=foo)6 p_list.append (i)7 I.start ()8 forIinchp_list:9 I.join ()TenEnd_time =time.time () One Print("totally time:{}". Format (End_time-start_time))
The correspondence between the scheme and practice:
Solution 1:--42.97582244873047
Solution 2:--44.63868308067322
Solution 3:--45.94909477233887
Solution 4:--15.7623131275177
Summarize:
The efficiency of multi_thread here is lower than single_thread!. Therefore, compute-intensive applications, do not use multi-threading, but increase the burden! (Gil-induced).
The most effective performance is the multi-process!
CPython concurrency in CPU-intensive applications