Python process and Thread and Gil (Global interpreter Lock)

Source: Internet
Author: User

Markdownpad Document process and thread

The program does not run alone, only the program is loaded into memory, the system allocates resources for it to run, and a program that is a dynamic execution of a data set is called a process. The difference between a program and a process is that a program is a set of instructions, which is a static descriptive text of the process, and a process is an execution activity of a program, which belongs to the dynamic concept. The process is generally composed of three parts: program, data set and process control block.

Why do we have to thread the process?

Because the process is still flawed:

    • A process can only do one thing at a time, and if you want to do two or more things at once, the process is powerless.

    • If the process is blocked during execution, such as waiting for input, the entire process hangs, even if some work in the process does not depend on the input data, it will not be able to execute

So what is a thread?

A thread is the smallest unit that the operating system can perform operations on. It is included in the process and is the actual operating unit of the process. A thread refers to a single sequential control flow in a process in which multiple threads can be concurrently, each thread performing different tasks in parallel

650) this.width=650; "src=" Http://i.imgur.com/acHaNqZ.png "alt=" Achanqz.png "/>

Relationship of process and thread:

    1. A thread can belong to only one process, while a process may have multiple threads, but at least one thread.

    2. A resource is assigned to a process, and all the threads of the same process share all the resources of that process.

    3. The CPU is assigned to a thread, that is, a thread that actually runs on the CPU.

    4. Process: Resource Management Unit (container), Thread: Minimum execution unit

Parallel and concurrency

Parallel Processing: It is a computational method that can perform two or more processing simultaneously in a computer system. Parallel processing can work on different aspects of the same program at the same time. The main purpose of parallel processing is to save time to solve large and complex problems.

concurrent Processing: refers to a period of time a few programs are in the start run to run, and these programs are running on the same processor (CPU), but at any point in time only one program on the processing Machine (CPU) run

Synchronous vs. asynchronous

synchronization: When a process executes a request, if it takes a while for the request to return information, the process waits until it receives the return information before it continues to execute.

Async: refers to a process that does not have to wait all the time, but continues to do the following, regardless of the state of other processes, when a message returns, the system notifies the process of processing, which can improve the efficiency of execution

About Gil (Global interpreter Lock)

The thread in Python is the native thread of the operating system, and the Python virtual machine uses a global interpreter lock (Global interpreter Lock) to mutex the use of the Python virtual machine. In order to support multithreading mechanism, a basic requirement is to implement the mutual exclusion of different threads to access shared resources, so the Gil is introduced.

GIL: After one thread has access to the interpreter, all other threads must wait for it to release the interpreter's access, even if the next instruction of those threads does not affect each other.

Before calling any Python C API, get the Gil

Gil Cons: Multiprocessor degenerate to single processor; Pros: Avoid a lot of locking unlock operations Python Multithreading: Due to the Gil Lock, resulting in the same moment, only one thread of the same process can be executed

Summarize:

For compute-intensive tasks (always using the CPU): Python multithreading doesn't work

For IO-intensive tasks (there are a lot of IO operations): Python multithreading is meaningful

To make Python use multicore: Only open processes, drawbacks: costly and switching complexity

The focus of using multicore should be: co- progression + multi-process

The ultimate idea: Change C module for multithreading

Threading Module
import threadingimport timedef countNum(n): # 定义某个线程要运行的函数    print("running on number:%s" %n)    time.sleep(3)if __name__ == ‘__main__‘:    t1 = threading.Thread(target=countNum,args=(23,)) #生成一个线程实例    t2 = threading.Thread(target=countNum,args=(34,))    t1.start() #启动线程    t2.start()    print("ending!")

Inherit thread-style creation:

import threadingimport timeclass MyThread(threading.Thread):    def __init__(self,num):        threading.Thread.__init__(self)        self.num=num    def run(self):        print("running on number:%s" %self.num)        time.sleep(3)t1=MyThread(56)t2=MyThread(78)t1.start()t2.start()print("ending")

Join () method and Setdaemon () method

# join (): The parent thread of this child thread will remain blocked until the child thread finishes running. # Setdaemon (True):         ' '         ' The thread is declared as a daemon thread and must be set before the start () method call, if not set to the daemon will be suspended indefinitely.         When we run a main thread in the program, if the main thread creates another child thread, the main thread and the child thread are suited two, running separately, then when the main thread completes         to exit , it verifies that the child thread is complete. 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 that as long as the main thread         complete, regardless of whether the child thread is complete, and the main thread to exit, then you can use the Setdaemon method "" "Import threadingfrom Time Import Ctime,sleepimport timedef Music (name):        print ("Begin listening to {name}. {time} ". Format (Name=name,time=ctime ()))        sleep (3)        print (" End Listening {time} ". Format (Time=ctime ())) def Blog (title):        print (" Begin recording the {title}. {time} ". Format (Title=title,time=ctime ()))        sleep (5)        print (' End Recording {time} '. Format (Time=ctime ())) threads = []T1 = Threading. Thread (target=music,args= (' FILL ME ',)) t2 = Threading. Thread (target=blog,args= (",)) threads.append (T1) threads.append (t2) If __name__ = = ' __main__ ':    # T2.setdaemon (true)    for T in Threads:         #t. Setdaemon (True) #注意: Always set   &N before start Bsp    t.start ()         #t. Join ()     #t1. Join ()     #t2. Join ()     #   Consider the results of these three join positions?    print ("All over%s"%ctime ())

Other methods

Thread实例对象的方法  # isAlive(): 返回线程是否活动的。  # getName(): 返回线程名。  # setName(): 设置线程名。threading模块提供的一些方法:  # threading.currentThread(): 返回当前的线程变量。  # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。  # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。

Python process and Thread and Gil (Global interpreter Lock)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.