Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand and master Python threads

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand and master Python threads". The content of the explanation is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn "how to understand and master Python threads".

1. Custom process

Customize the process class, inherit the Process class, override the run method (override the run method of Process).

From multiprocessing import Processimport timeimport osclass MyProcess (Process): def _ _ init__ (self, name): # # rewrite, need _ _ init__, also added a new parameter. # # Process.__init__ (self) cannot be omitted Otherwise, the error message: AttributeError:'XXXX'object has no attribute'_ colsed' Process.__init__ (self) self.name = name def run (self): print ("child process (% s mi% s) start"% (self.name, os.getpid ()) time.sleep (3) print ("child process (% s mi% s) ends"% (self.name) Os.getpid ()) if _ _ name__ = ='_ main__': print ("parent process starts") p = MyProcess ("Ail") # automatically calls MyProcess's run () method p.start () p.join () print ("parent process ends")

# output results

The parent process starts

Child process (Ail-38512) start

Child process (Ail-38512) ends

Parent process ends

two。 Processes and threads

Multi-processes are suitable for CPU-intensive operations (there are more CPU operation instructions, such as scientific computing, floating-point computing with many digits).

Multithreading is suitable for IO-intensive operations (those with more read and write data operations, such as crawlers, file uploads, downloads)

Threads are concurrent and processes are parallel: processes are independent of each other and are the smallest unit of resources allocated by the system, and all threads in the same process share resources.

Process: a running program or code is a process, and a code that is not running is called a program. The process is the smallest unit of resource allocation in the system, and the process has its own memory space, so the data between processes are not shared and the overhead is high.

A process is a dynamic execution process of a program. Each process has its own address space, memory, data stack, and other auxiliary data for tracking execution. The operating system is responsible for the execution of all processes on it, and the operating system allocates execution time to these processes reasonably.

Thread: the smallest unit of scheduling execution, also known as the execution path, cannot exist independently and depends on the existence of a process. A process has at least one thread, called the main thread, and multiple threads share memory (data sharing and global variables). Thus improve the efficiency of the program.

Thread is the smallest unit that the operating system can schedule. It is included in the process and is the actual operating unit in the process. A thread refers to a single order of control flow in a process, where multiple threads can be concurrent in a process, and each thread performs different tasks in parallel. A thread is an execution context (execution context), that is, a string of instructions that a CPU needs to execute.

Main thread: the main thread is the first thread generated in the thread creation process, that is, the thread corresponding to the main function.

Cooperative program: a lightweight thread in user mode, scheduling is controlled by the user, has its own register context and stack, switching basically has no kernel switching overhead, and switching is flexible.

The relationship between processes and threads

3. Multithreading

The operating system dispatches threads by assigning time slices to different threads (CPU running time). When CPU executes a time slice of one thread, it will quickly switch to the next thread. The time slice is so short and the switching speed is so fast that the user is not even aware of it. Multiple threads are executed by CPU in turn according to the allocated time slices. Nowadays, the CPU of most computers is multi-core. Under the scheduling of the operating system, multiple threads can be executed in parallel by multiple CPU. The execution speed of the program and the utilization efficiency of CPU are greatly improved. Most of the mainstream logarithmic programming languages can support multithreading very well, however, Python cannot implement multithreading because of GIL locks.

Threads in memory

4.Thread class method

(1) start ()-- start executing the thread

(2) run ()-A method that defines a thread (developers can override it in a subclass); the standard run () method initiates a call to the callable object (if any) passed to the object constructor as a target parameter, along with location and keyword parameters obtained from the args and kwargs parameters, respectively.

(3) join (timeout=None)-hangs until the started thread terminates; it is blocked unless a timeout (in seconds) is given; because join () always returns None, you have to call is_alive () after join () to determine whether a timeout occurs-- if the thread is still alive, join () times out. A thread can be join () many times. Join () causes a RuntimeError exception if an attempt to join the current thread results in a deadlock. If you try to join () a thread that hasn't started yet, the same exception will be thrown.

(4) is_alive ()-Boolean value indicating whether the thread is still alive; when the run () method starts until the end of the run () method, this method returns True.

(5) threading.current_thread ()-returns the Thread object that currently corresponds to the caller's control thread. For example, get the name of the current thread, which can be current_thread (). Name.

5. Multi-thread and multi-process small Casefrom threading import Threadfrom multiprocessing import Processimport osdef work (): print ('hello,',os.getpid ()) if _ _ name__ = =' _ _ main__': # start multiple threads under the main process Each thread is the same as the pid of the main process T1 = Thread (target=work) # start one thread T2 = Thread (target=work) # start two threads t1.start () # # start ()-- It must be called at most once per thread object.It arranges for the object's run () method to be # # invoked in a separate thread of control.This method will raise a RuntimeError if called more than once on the # # same thread object. T2.start () print ('main thread / main process pid',os.getpid ()) # opens multiple processes, each with a different life cycle of pid p1 = Process (target=work) p2 = Process (target=work) p1.start () p2.start () print (' main thread / main process pid',os.getpid ()) 6.Thread

The states of the thread include: create, ready, run, block, end.

(1) when creating an object, it represents that the Thread is initialized internally.

(2) after calling the start () method, thread will enter the queue to prepare for running, which is called ready state before obtaining CPU and memory resources; polling to obtain resources and entering the running state; if sleep is encountered, it will enter the blocking state

(3) when the normal operation of the thread code ends or encounters an exception, the thread will terminate.

7. Custom thread

(1) define a class that inherits Thread

(2) rewrite _ _ init__ and run ()

(3) create thread class object

(4) start thread.

Import timeimport threadingclass MyThread (threading.Thread): def _ init__ (self,num): super (). _ _ init__ () # or Thread.__init__ () self.num = num def run (self): print ('thread name:', threading.current_thread (). GetName (), 'parameters:', self.num, 'start time:' Time.strftime ('% Y-%m-%d% HRV% MRV% S') if _ _ name__ = ='_ _ main__': print ('main thread start:' Time.strftime ('% Y-%m-%d% HRV% MVA% S')) T1 = MyThread (1) T2 = MyThread (2) t1.start () t2.start () t1.join () t2.join () print ('main thread end:', time.strftime ('% Y-%m-%d% HRV% MJV% S')) 8. Threads share data with GIL (global interpreter lock)

If it is a global variable, each thread is shared

GIL lock: can be used to simulate the scene of a basketball game, the basketball court as CPU, a basketball game as a thread, if there is only one basketball court, multiple games will be queued, similar to a simple single-core multi-threaded program; if there are multiple basketball courts, multiple games are held at the same time, it is a simple multi-core multi-threaded program. However, the Python has a special rule: each game must be under the supervision of the referee, and there is only one referee. In this way, no matter how many basketball courts you have, only one court is allowed to play at a time, the other venues will be idle, and other games will have to wait.

9.GIL and Lock

GIL guarantees that a process can have multiple threads at a time, but only one thread is executing; the purpose of the lock is to protect the shared data, and only one thread can modify the shared data at a time.

Class is threading.Lock

It has two basic methods, acquire () and release ().

When the state is non-locked, acquire () changes the state to locked and returns immediately. When the state is locked, acquire () blocks to other threads calling release () to change it to an unlocked state, and then the acquire () call resets it to locked state and returns.

Release () is called only in the locked state; it changes the state to non-locked and returns immediately. If you try to release an unlocked lock, a RuntimeError exception is thrown.

Caese is as follows:

From threading import Threadfrom threading import Lockimport timenumber = 0def task (lock): global number lock.acquire () # # hold lock for i in range (100000) number + = 1 lock.release () # # release lock if _ _ name__ = ='_ main__': lock=Lock () T1 = Thread (target=task,args= (lock,)) T2 = Thread (target=task,args= (lock,)) T3 = Thread (target=task,args= (lock) ) t1.start () t2.start () t3.start () t1.join () t2.join () t3.join () print ('number:',number) 10. Semaphore of the thread

Class threading.Semaphore ([values])

Values is an internal count, and values defaults to 1. If it is less than 0, a ValueError exception is thrown, which can be used to control the number of threads and concurrency.

The implementation of semaphore:

S=Semaphore (?)

There is an internal counter counter, and the value of counter is the number of threads that can be opened at a time. Every time we s.acquire (), the counter is subtracted by 1, and every time we s.release (), the counter is added by 1. When the counter is 0, other threads are waiting.

The program adds a counter function (semaphore) to limit the number of threads at a point in time and prevent the program from crashing or other exceptions.

Case

Import timeimport threadings=threading.Semaphore (5) # add a counter def task (): s.acquire () # counter acquires lock time.sleep (2) # Program hibernates for 2 seconds print ("The task run at", time.ctime ()) s.release () # counter releases lock for i in range (40): t1=threading.Thread (target=task,args= ()) # create thread t1.start () # start thread

You can also use the with operation to replace acquire () and release (), and the above code is adjusted as follows:

Import timeimport threadings=threading.Semaphore (5) # add a counter def task (): with s: # # similar to the with operation of opening a file # # s.acquire () # counter acquires lock (2) # program hibernates for 2 seconds print ("The task run at", time.ctime ()) # # s.release () # counter releases lock for i in range (40): t1=threading.Thread (target=task Args= () # create thread t1.start () # start thread

With is recommended.

Thank you for your reading, the above is the content of "how to understand and master Python threads". After the study of this article, I believe you have a deeper understanding of how to understand and master Python threads, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report