Thread of three processes

1. Thread definition

- thread is the smallest unit of operation scheduling by the operating system

- included in the process, which is the actual operation unit of the process

- the process itself cannot execute by itself. To operate the cpu, you must create a thread, which is a collection of instructions

- a thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread performs different tasks

- no matter how many threads you start and how many CPUs you have, python will always be calm when executing. Only one thread is allowed to run at the agreed time

- the process itself cannot be executed. To operate the cpu, you must create a thread, which is a collection of instructions

- all threads in the same process share the same memory space, and the memory space of different processes is different

- multiple threads in the same process can access resources from each other, threads can operate other threads in the same process, and a single process can only {operate sub processes

- if two processes want to communicate, they must pass through an intermediate agent

2. Difference between process and thread

1. Threads contain processes

2. Process shared memory space

3. Process memory is independent

4. Processes can generate sub processes, which cannot be accessed by each other

5. In a process, threads can communicate with each other. If the two processes want to communicate, they must be realized through a middleware agent

6. Creating a new thread is very simple. Creating a new process requires cloning its parent process

7. Threads can help applications do several things at the same time

8. The parent process can be modified without affecting the child process, but it cannot be modified

3.for loop starts multiple threads at the same time

import threading
import time
def sayhi(num):
    print("running on number:%s"%num)
    time.sleep(3)
for i in range(50):
    t = threading.Thread(target=sayhi,args=('t-%s'%i,))
    t.start()

4.GIL lock and thread lock

4.1 GIL global interpreter lock

- under the python global interpreter, ensure that only one thread runs at the same time

- prevent multiple threads from modifying data

4.2 GIL lock and thread lock

- GIL lock can only ensure that one thread can operate on a resource at the same time, but it can be released when a thread has not finished executing

- the essence of thread lock is to add a mutex lock to the data in the thread

- after the thread lock is added, all threads cannot read this data

- why do you need thread locks with GIL global interpreter

- because the cpu is time-sharing

4.3 in the case of GIL, the cause of error will be resolved if {count = count + 1 is executed. Use the method solved by thread

# 1) Step 1: count = 0. The initial value of count is 0
# 2) Step 2: to add 1 to count, thread 1 first applies for the GIL global interpreter lock
# 3) Step 3: call the native thread of the operating system to execute in the operating system
# 4) Step 4: count plus 1 has not been executed yet. It's time to be asked to release GIL
# 5) Step 5: after thread 1 releases the GIL, thread 2 also needs to operate on the count. At this time, thread 1 has not finished executing, so the count is still 0
# 6) Step 6: when thread 2 gets count = 0, it also needs to add 1 to count. If thread 2 executes quickly, it will be completed at one time
#    Count plus 1, then count changes from 0 to 1
# 7) Step 7: after adding 1, thread 2 assigns count=1 and releases GIL
# 8) Step 8: after the execution of thread 2, the cpu gives it to thread 1. Thread 1 continues to perform the count plus 1 operation according to the context and gets the GIL first
#    Lock and complete the operation of adding 1. Since the data count obtained by thread 1 first = 0, the result is still 1 after adding 1
# 9) Step 9: thread 1 assigns count=1 to count and releases the GIL lock. At this time, even threads add 1 to the data, but the final value is 1

5.join() and setdamon()

5.1join()

- execute the main thread after all threads are executed

import threading
import time
start_time = time.time()

def sayhi(num): #Define the function to be run by each thread
    print("running on number:%s" %num)
    time.sleep(3)

t_objs = []    #Store the process instance object in this list
for i in range(50):
    t = threading.Thread(target=sayhi,args=('t-%s'%i,))
    t.start()          #Start a thread and the program will not block
    t_objs.append(t)
print(threading.active_count())    #Print the number of currently active processes
for t in t_objs: #Use the for loop to wait for the completion of all the above 50 processes
    t.join()     #Block a program
print(threading.current_thread())    #Print the process of executing this command

print("----------------all threads has finished.....")
print(threading.active_count())
print('cost time:',time.time() - start_time)

5.2  setDaemon()

import threading
import time
start_time = time.time()

def sayhi(num): #Define the function to be run by each thread
    print("running on number:%s" %num)
    time.sleep(3)
for i in range(50):
    t = threading.Thread(target=sayhi,args=('t-%s'%i,))
    t.setDaemon(True)  #To turn the current thread into a daemon thread, it must be set before t.start()
    t.start()          #Start a thread and the program will not block
print('cost time:',time.time() - start_time)

 

 

Added by nicob on Sun, 30 Jan 2022 21:58:04 +0200