In Python, each process has a Gil lock, causing each process to have only one thread to execute at the same time
Threading Module
Import Threadingimport timedef foo (x): print ("--------%s"%x) Time.sleep (3) t1=threading. Thread (target=foo,args= (2,)) #创建线程对象; task, parameter, tuple form T1.start () # start print ("Ending")
Join
Blocks the main thread, waits for the thread to end before continuing to execute the main thread
Import Threadingimport timedef foo (x): print ("--------%s"%x) Time.sleep (3) def bar (n): print ("--------% S "%n) Time.sleep (5) s = Time.time () t1=threading. Thread (target=foo,args= (2,)) #创建线程对象; task, parameter, tuple form T1.start () # start t2=threading. Thread (target=bar,args= (5,)) #创建线程对象; task, parameter, tuple form T2.start () # start T1.join () T2.join () print ("Ending") print ( Time.time ()-s) #----------------AA ending# 5.001526832580566
Do not open multi-threaded run time
Import Threadingimport timedef foo (x): print ("--------%s"%x) Time.sleep (3) def bar (n): print ("------- -%s "%n) Time.sleep (5) s = time.time () foo (2) bar (5) print (" Ending ") print (Time.time ()-s) #----------------AA ending# 8.000638246536255
Setdaemon (True) daemon thread
The thread is declared as a daemon thread and must be in the start ()
The method is set before the call, and if not set to the daemon, the thread is suspended indefinitely.
When we execute a main thread in a program run, if the main thread creates another child thread, the main thread and the child thread
Just suited two, run separately, then when the main thread completes
When you want to exit, the child thread is checked for completion. If the child thread is not completed, the main thread waits for the child thread to complete before exiting. But sometimes what we need is just the main thread
Completed, regardless of whether the child thread is completed, and the main thread to exit, then you can
With the Setdaemon method.
When Daemon is set to true, if the main thread exits, the child thread will also exit, and the child thread will continue to run until normal exit
Import Threadingimport timedef foo (x): time.sleep (3) print ("--------%s"% x) def bar (n): time.sleep (5) print ("--------%s"% n) s = time.time () t1=threading. Thread (target=foo,args= (2,)) #创建线程对象; task, parameter, tuple form # T1.setdaemon (True) T1.start () # start t2=threading. Thread (target=bar,args= (5,)) #创建线程对象; task, parameter, tuple form T2.setdaemon (True) T2.start () # start print ("Ending") print ( Time.time ()-s) # ending# 0.0010097026824951172#--------2
Thread instance object's method # isAlive (): Returns whether the thread is active. # getName (): Returns the thread name. # SetName (): Sets the thread name. Some of the methods provided by the threading module are: # threading.currentthread (): Returns the current thread variable. # threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends. # Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).
==================
Compute-intensive and IO-intensive
IO-intensive:
There are a lot of IO operations in the program
Computationally dense:
There are a number of computational operations (non-IO operations) in the program
For compute-intensive, multithreading is not as sequential as execution; Gil Locks can only come out one thread, and multiple threads can only switch execution, so that the computational-intensive efficiency becomes lower
For IO-intensive, the switch executes other threads for IO blocking, which saves blocking time and high efficiency
Conclusion: For Python, the same process can not take advantage of the multi-core advantage, multithreading processing IO-intensive tasks, has the advantage, for processing computationally intensive tasks, is not recommended to use
35. Python Threads