python--process/Thread

Source: Internet
Author: User

Process/Thread1 Process

Operating system process switching:

1 IO operation occurs:

2 Fixed time:

2 Process Definition (Resource Management Unit)---(container)

Process is a program on a data set of a dynamic execution process, the process is generally composed of programs, data sets, Process control block three parts.

Implementing the Concurrency Process

Processes are independent of each other.

3 Threads (minimum execution unit)

Threads appear to reduce the consumption of context switches, provide concurrency to the system, and break through a process that can only do the same thing, making it possible to be in-process concurrency.

4 process-to-thread relationships

A process is a running activity on a data set by a program on a computer, which is a system that allocates resources

and the basic unit of dispatch, is the basis of operating system structure

A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process.

Process-to-thread relationship:

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

(2) Resources are allocated to the process, and all the threads of the same process share all the resources of the process

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

5 Parallel and concurrent

Parallel processing (Parallel processing)

is a computing method that can perform two or more processing simultaneously in a computer system. Parallel processing can work concurrently with different aspects of the unified program. The main purpose of parallel processing is to save time to solve large and complex problems

Concurrent processing (concurrency processing)

Refers to a time period in which several programs are running to completion, and these programs are running on the same processor, but only one program at any point on the processing Machine (CPU) runs

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is that you have the ability to handle multiple tasks at the same time, so that parallelism is a subset of concurrency

Attention:

Python failure Multithreading: Because Gil causes the same moment, only one thread of the same process can be run. Multiple processes can be parallel, and multithreading cannot be parallel.

6 synchronous vs. asynchronous

Sync: in the computer field, synchronization means that when a process executes a request, if the request takes a while to return the information, the process waits until the return message is received before it continues .

Async: it means that the process does not have to wait all the time, but continues to perform operations on the polygon, regardless of the state of other processes, and notifies the process when a message returns .

7 Threading Module

The thread class executes the creation:

Import Threadingimport Timedef Listen ():    print ("Listen to the Music")    Time.sleep (3)    print ("Listne stop") Def write ():    print ("Writing blog")    time.sleep (5)    print ("Writing stop") t1=threading. Thread (Target=listen) #生成一条线程t2 =threading. Thread (Target=write) #生成第二条线程t1. Start () #启动线程t2. Start () print ("Ending") #主线程执行效果: Listen to the musicwriting Blogendinglistne stopwriting Stop

The thread class inherits the 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 (T2=mythread) T1.start () T2.start () print ("Ending") execution result: running on number:56running on Number:73ending

  

Instance methods for the thread class:

Join ()

T.join (): Thread object T is not finished, it will block your main threads. Not related to child threads

Import threadingimport timefrom time import ctime,sleepdef Music (name):    print ("Begin listening to {name}". Format ( Name=name,time=ctime ()))    Sleep (3)    print ("End Listenting{time}". Format (Time=ctime ())) def Blog (title):    print ("Begin recording the {title}". Format (Title=title,time=ctime ()))    Sleep (5)    print ("End recording The {time} '. Format (Time=ctime ())) threads=[]t1=threading. Thread (target=music,args= ("FILL ME",)) t2=threading. Thread (target=blog,args= ("Python")) threads.append (T1) threads.append (t2) If __name__ = = ' __main__ ': for    T in Threads:        T.start ()        # t.join () # finally appears main program etc T1  T2 after execution    # t1.join ()  #t1线程不结束 Other threads cannot execute first T1,T2 The main thread T2.join () is then executed after the execution of the T1    () #t1 T2 after the execution of the master program print    ("All over%s"%ctime ())

Setdeamon (True)  

The thread is declared as a daemon thread and must be set before the start () method call, if it is not set to a daemon, it will be indefinitely suspended.

When we are running the program, we execute a main thread, if the main thread threads create a sub-thread to separate two paths, run separately, then when the main thread completes

When you want to exit, it verifies that the child thread is complete, and 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 just the main thread

Completed, regardless of whether the child thread is completed, and the main thread to exit, then you can use the Setdeamon method

Import threadingimport timefrom time import ctime,sleepdef Music (name):    print ("Begin listening to {name}". Format ( Name=name,time=ctime ()))    Sleep (3)    print ("End Listenting{time}". Format (Time=ctime ())) def Blog (title):    print ("Begin recording the {title}". Format (Title=title,time=ctime ()))    Sleep (5)    print ("End recording The {time} '. Format (Time=ctime ())) threads=[]t1=threading. Thread (target=music,args= ("FILL ME",)) t2=threading. Thread (target=blog,args= ("Python")) threads.append (T1) threads.append (t2) If __name__ = = ' __main__ ':    # The daemon thread follows the main thread, T1 T2 Two are set as the daemon thread, and the main thread executes no other than the    # T1.setdaemon (True)  #把t1设置为守护线程, waits for the T2 execution to finish, but T1 also needs to execute, normal execution    T2.setdaemon (True) #把t2设置为守护线程, the main thread ends, T2 ends, and the for T in Threads is not executed    : #for遍历        t.start ()      print ("All over% S "%ctime ())

Other methods:

methods for thread instance objects

IsAlive (): Returns whether the thread is active

GetName (): Returns the thread name

SetName (): Set thread name

Some of the methods provided by the threading module are:

Threading.currentthread (): Returns the current thread variable.

Threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.

Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).

8 GIL (Global explanation 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. To support multithreading, a basic requirement is to implement mutual exclusion for shared resource access by different threads, all introducing the Gil.

Added to the C Python explanation, in order to support the multithreading mechanism, a basic requirement is to implement different threads to the shared resource access mutually exclusive, so introduced the Gil.

GIL: After a thread has access to the interpreter, all other threads must wait for him 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: Multi-processor degenerate to single processor; priority: Avoid a large number of additional unlocking operations

The impact of the Gil

No matter how many threads you start, the number of Cpu,python you have to talk to during the execution of a process will only allow a single thread to run at the same time.

All, Python is not able to utilize multi-core CPUs for multithreading

In this way, Python is not as efficient as serial (without a lot of switching) in multithreading for computer-dense tasks, but there is a significant increase in efficiency for IO-intensive tasks.

Import Timedef cal (n):    sum=0 for    i in range (n):        sum+=is=time.time () # import threading# t1=threading. Thread (target=cal,args= (10000000,)) # t2=threading. Thread (target=cal,args= (10000000,)) # # T1.start () # T2.start () # T1.join () # T2.join () # # time 10.940000057220459# # time 1.1090002059936523cal (10000000) cal (10000000) print ("Time", Time.time ()-s) # Time 10.986999750137329# time 1.128000020980835

Compute intensive: Always using the CPU

IO: A large number of IO operations exist

Summary: For compute-intensive tasks: Python multithreading does not work

For IO-intensive tasks: Python multithreading is meaningful

Python uses multicore: Open process, cons: Overhead and switching complexity

python--process/Thread

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.