In the 57----threding module

python, each process has a GIL lock, which causes each process to have only one thread executing at the same time.

threadingModule

import threading
Import time
Def foo(x):
    Print("--------%s" %x)
    Time.sleep(3)
 
T1=threading.Thread(target=foo,args=(2,)) #Create thread object; task, parameter, tuple form
T1.start() #Start 

join

to block the main thread, wait for the thread to finish before continuing to execute the main thread

import threading
Import time
Def 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,)) #Create thread object; task, parameter, tuple form
T1.start() # start
T2=threading.Thread(target=bar,args=(5,)) #Create thread object; task, parameter, tuple form
T2.start() # start
 
T1.join()
T2.join()
 
Print("ending")
Print(time.time() - s)
 
# --------2
# --------5
# ending
# 5.001526832580566

Do not open multi-threaded runtime

import threading
import time
def 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)
 
# --------2
# --------5
# ending
# 8.000638246536255

setDaemon(True) The daemon thread

declares the thread as a daemon thread, which must be set before the start() method call, if not set to the daemon thread The program will be suspended indefinitely. When we run the program, we execute a main thread. If the main thread creates another sub-thread, the main thread and the sub-thread are split and run separately. When the main thread finishes wanting to exit, it will check. Whether the child thread is completed. If the child thread is not completed, the main thread will wait for the child thread to complete before exiting. But sometimes what we need is that as long as the main thread is completed, regardless of whether the sub-thread is completed, it must be with the main thread to exit, then you can use the setDaemon method

when the daemon is set to true, if the main thread exits, Then the child thread will also exit, otherwise, the child thread will continue to run until it exits normally

mport threading
Import time
Def 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,)) #Create thread object; task, parameter, tuple form
# t1.setDaemon(True)
T1.start() # start
T2=threading.Thread(target=bar,args=(5,)) #Create thread object; task, parameter, tuple form
t2.setDaemon(True)
T2.start() # start
 
 
Print("ending")
Print(time.time() - s)
 
# ending
# 0.0010097026824951172
# --------2
@Thread instance method
  # isAlive(): Returns whether the thread is active.
  # getName(): Returns the thread name.
  # setName(): Set the thread name.
 
Some methods provided by the threading module:
  # threading.currentThread(): Returns the current thread variable.
  # threading.enumerate(): Returns a list containing the running threads. Running After the thread starts and ends, it does not include the thread before and after the start.
  # threading.activeCount(): Returns the number of threads that are running, with the same result as len(threading.enumerate()). 

==================

Compute-intensive and IO-intensive

IOIntensive:

There are a lot of IO operations in the program

Compute intensive:

There are a lot of computing operations in the program (non-IO operations )

For computationally intensive, multithreading is not as good as sequential execution; GIL locks can only come out one thread, multiple threads can only switch execution, which will become less efficient for computationally intensive

Type, for IO blocking, switch to execute other threads, which saves the blocking time, the efficiency will be high

Conclusion: For Python, under the same process, you can not take advantage of multi-core, multi-thread processing IO-intensive Advantages when using tasks, not recommended for tasks that are computationally intensive