thread

Getting to know the thread

In traditional operating systems, each process has an address space, and there is a thread of control by default. The real execution unit of CPU is thread.

like

In a factory, each workshop has a house, and this house is the memory space. By default, each workshop has an assembly line, which is a thread.

Operating system == "factory"

Process == "Workshop

Thread == "Pipeline

cpu==>Power

Thread: the smallest execution unit of CPU

Process: Resource Collection/Resource Unit

thread running = running code

Process running = various resources + threads

Run: apply for memory space, first put the interpreter in and put the code in, and run the code.

The difference between process and thread:

Thread == "Single refers to the execution process of the code

Process=="resource application and destruction process

Process memory spaces are isolated from each other, and threads under the same process share resources.

Process and thread creation speed comparison:

Process creation speed is much slower than thread creation speed

Because the process needs to apply for resources to open up space, it is very slow, and the thread just tells the operating system an execution plan, which is very fast and does not need to open up new space.

Two ways to start child threads

one,

from threading import Thread
import time
def task(): print('线程 start') time.sleep(2) print('线程 end') if __name__ == '__main__': t = Thread(target=task) t.start() # 告诉操作系统开一个线程 . print('主')

two,

from threading import Thread
import time
# 进程等待所有线程结束才会结束 class Myt(Thread): def run(self): print('子线程 start') time.sleep(5) print('子线程 end') t = Myt() t.start() print('主线程')

You will find that it is very similar to the process, almost the same.

Then there will also be a join method

Thread join method

from threading import Thread
import time
def task(): print('子线程 start') time.sleep(2) print('子线程 end') t = Thread(target=task) t.start() t.join() # 等待子线程运行结束 print('主线程') 

It is the same as the process. After waiting for a sub-thread to end, the blocking of the join will end.

space sharing

from threading  import Thread
import time,os

x = 100 def task(): global x x = 50 print(os.getpid()) # 5204 if __name__ == '__main__': t = Thread(target=task) t.start() time.sleep(2) print(x) # 50 print(os.getpid()) # 5204 

Multiple child threads share the same memory space, that is, the space of the same process. A child thread changes the objects in the space, and other child threads access the things that have been changed.

When process join meets thread join

from multiprocessing import Process
from threading import Thread import time def task(): print('进程 开启') time.sleep(10) print('进程 结束') def task2(): print('子线程 开启') # time.sleep(2) print('子线程 结束') if __name__ == '__main__': p = Process(target=task) t = Thread(target=task2) t.start() # 开线程 p.start() # 开进程 print('子进程join开始') p.join() # 主进程的主线程等待子进程运行结束 print('主')

The p.join() in the main process will only block the main thread, not the child thread.

Other uses of threads (other methods)

from threading import Thread,currentThread,enumerate,activeCount
# import threading
import time # threading.current_thread() # threading.current_thread() def task(): print('子线程 start') time.sleep(2) print('子线程 end') print(enumerate()) # print(currentThread(),'子线程') if __name__ == '__main__': t1 = Thread(target=task) t2 = Thread(target=task) t1.start() t2.start() # print(t1.is_alive()) # True # print(t1.getName()) # Thread-1 # print(t2.getName()) # Thread-2 # t1.setName('班长') # print(t1.getName()) # print(currentThread().name) # print(enumerate()) # [<_MainThread(MainThread, started 1856)>, <Thread(Thread-1, started 6948)>, <Thread(Thread-2, started 3128)>] # print(activeCount()) # 3 # print(len(enumerate())) # 3

is_alive(): Determine if the thread is alive or dead

get_Name(): get the name of the thread

set_Name(): Modify the name of the thread.

currentThread(): returns the name of the current thread

enumerate(): Returns currently surviving threads, excluding unopened and ended ones, in the form of a list

activeCount(): Returns the number of active threads

Daemon thread

So this is very similar to the daemon process.

# 守护线程 守护的是进程的运行周期
from threading import Thread,enumerate,currentThread
import time def task(): print('守护线程开始') print(currentThread()) time.sleep(20) # print('守护线程结束') def task2(): print('子线程 start') time.sleep(5) print(enumerate()) print('子线程 end') if __name__ == '__main__': t1 = Thread(target=task) t2 = Thread(target=task2) t1.daemon = True t2.start() t1.start() print('主') 

1. The difference between a thread and a process that has finished running:

​ 1. The completion of the main process refers to the completion of the main process code.

​ 2. When the main thread finishes running, it means that after all non-daemon threads in the process where it is located have finished running, the main thread is considered to have finished running.

​ Emphasis: the operation is completed, not terminated

2. Daemon process: After the main process code runs, the daemon process ends (the main process is the guardian)

​ The main process must wait for all non-daemon processes to run before reclaiming the resources of the child process (otherwise a zombie process will be generated) before ending.

​ The main process and other sub-processes are because the main process needs to collect corpses for the sub-processes (instead of using the wait method to initiate a resource recovery signal (pid number, status information) to the operating system)

Daemon process: After the main process code runs, the daemon process ends

Daemon thread: After the non-daemon thread runs, the daemon thread ends

Whether it is a process or a thread, follow: the guardian xxx will wait for the main xxx to be destroyed after running

It should be emphasized that: the completion of the operation does not terminate the operation

#1. For the main process, the completion of the operation refers to the completion of the main process code . 
#

explain in detail:

#1 The main process has finished running after its code ends (the daemon process is recycled at this time), and then the main process will wait for the non-daemon sub-processes to run and recycle the resources of the sub-process (otherwise, it will generate zombie process), will end,

#2 The main thread is not finished until other non-daemon threads have finished running (daemon threads are recycled at this point). Because the end of the main thread means the end of the process, the overall resources of the process will be reclaimed, and the process must ensure that all non-daemon threads are running before it can end.

Reprinted in: https://www.cnblogs.com/whnbky/p/11537310.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326559968&siteId=291194637