Python basis (xiv) - Concurrent Programming

First, the operating system

Reference documents: https://www.cnblogs.com/yuanchenqi/articles/6248025.html

Second, the process and thread

2.1 Introduction to Process

Process : a program execution time in a dynamic data set, generally by the program, the data set, the process control block of three parts

  • Procedure: To complete what used to describe the process and how to complete
  • Data collection: resources during execution of the program need to use
  • Process control block: the external characteristics of the recording process, the change process performed in the process described, the system can use it to manage and control the process, it is the only sign of the presence sensing system processes

2.2 thread

Thread : lightweight processes, it is a basic execution unit CPU, is the minimum unit during program execution, by a thread ID, the program counter, a register set and stack composed. The introduction of the thread reduces the overhead of concurrent execution of the program to improve the concurrent performance of the operating system. Thread does not have its own system resources.

2.3, the difference between processes and threads

1) a program of at least one process, a process has at least one thread. (Process can be understood as a container thread)

2) process has a separate memory unit in the implementation process, and multiple threads of shared memory , thereby greatly enhancing the efficiency of the program.

3) threads in the implementation process and the process is different. Each entry has a separate thread running, sequences and procedures performed in the order outlet. But the thread is not able to execute independently, it must exist according to the application, providing multiple execution threads controlled by the application.

4) process is a program with a certain separate functions run on one set of data on the activities of the process is a stand-alone system unit resource allocation and scheduling.

5) A thread is a physical process, is the basic unit of CPU scheduling and dispatching, which is smaller than the process of the basic unit can operate independently. Basically thread does not own its own system resources, with only essential point in the operation resource (such as a program counter, a set of registers and stack) but it can be shared with other threads process belong to a process that is owned by all of the resources.

  6) A thread can be created and destroyed another thread; can execute concurrently across multiple threads in the same process.

Three, python GIL

In CPython, the global interpreter lock(全局解释器锁), or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

The core means that, no matter how many threads you start, you have a number of cpu, Python at the time of execution will only allow one thread running at the same time

Four, python thread and threading modules

4.1, thread calls the way

1) direct call mode

Threading Import 
Import Time 

DEF FUNC (NUM): # define the function of each thread run 
    Print ( "ON running NUM:% S"% NUM) 
    the time.sleep (. 3) 

IF the __name__ == '__main__': 
    T1 = of the threading.Thread (target = func, args = ( 1,)) # generates a thread instance, target = note function name can only write 
    T2 of the threading.Thread = (target = FUNC, args = (2,)) 

    t1.start () # start a thread 
    t2.start () 

    Print (t1.getName ()) # get the thread name ==> the thread-1 
    Print (t2.getName ()) # get the thread name ==> thread-2

2) inherited invocation

Threading Import 
Import Time 

class MyThread (threading.Thread): 
    DEF __init __ (Self, NUM): 
        threading.Thread .__ the init __ (Self) # parent class initialization 
        self.num = NUM 
    DEF RUN (Self): # define each thread to run function name must be RUN 
        Print ( "ON running NUM:% S"% self.num) 
        the time.sleep (. 3) 

IF the __name__ == '__main__': 
    T1 = the MyThread (. 1) 
    T2 = the MyThread (2) 
    T1. Start () 
    t2.start () 
    Print ( "Ending ..........")

4.2, threading.Thread example method

1) the Join (): Before the child thread finishes running, the parent of the child thread thread will be blocked

2) setDaemon (True): The thread is declared as a daemon thread must be set before the method is called start (), if you do not set a daemon thread hangs the program will be unlimited. This method is basic and join the opposite. When we run the program, the implementation of a main thread, the main thread if they create a child thread, the main thread and the child thread running respectively on the two forces were divided, so when the main thread finishes want to quit, will test whether the child thread to complete. If the child thread is not completed, the main thread will wait for the child threads to complete before exit. But sometimes we need is just the main thread finishes, regardless of whether the child thread to complete, and should be the main thread exits together, then you can use the method setDaemon

import threading
from time import ctime,sleep
import time

def ListenMusic(name):

        print ("Begin listening to %s. %s" %(name,ctime()))
        sleep(3)
        print("end listening %s"%ctime())

def RecordBlog(title):

        print ("Begin recording the %s! %s" %(title,ctime()))
        sleep(5)
        print('end recording %s'%ctime())

threads = []   #定义线程列表
t1 = threading.Thread(target=ListenMusic,args=('AA',))
t2 = threading.Thread(target=RecordBlog,args=('BB',))
threads.append(t1)
threads.append(t2)

if __name__ == '__main__':

    for t in threads:
        t.setDaemon (True) # Note: Be sure to set up before the start
        t.start()
        # t.join()
    t1.join()
    # t1.setDaemon(True)

    print ("all over %s" %ctime())

3) Other methods

run (): # thread thread object is automatically executed after being run cpu scheduling method 
start (): # start thread activity. 
isAlive (): # Returns the thread is active. 
getName (): # Returns the thread name. 
setName (): # Set the thread name. 

Some methods #threading module provides: 
threading.currentThread (): # Returns the current thread variable. 
threading.enumerate (): # returns a list that contains the running thread. Refers to the thread starts running, before the end, it does not include a thread before starting and after termination. 
threading.activeCount (): # Returns the number of running threads, and len (threading.enumerate ()) have the same result.

Fifth, genlock

5.1, leads to problems

Time Import 
Import Threading 

DEF addNum (): 
    , Ltd. Free Join # NUM get all the global variables in each thread 
    # num-#final NUM = 1: 0 

    the TEMP = NUM 
    #Print ( '- GET NUM:', NUM) 
    Time. sleep (0.01) # io equivalent operations, the thread switch 
    num = temp-1 # 1 operation of this public variables 

num = 100 # to set a shared variable 
thread_list = [] 
for I in Range (100): 
    T = of the threading.Thread (target = addNum) 
    t.start () 
    thread_list.append (T) 

for T in thread_list: # wait for all threads is finished 
    t.join () 

Print ( 'Final NUM:', NUM) 

# result 
# time when .sleep (0.1) ==> 99 
# when time.sleep (0.01) ==> 90 appear on multiple random
 
# reasons: when the first thread to get num = 100, when the time.sleep, thread switch, a second or 100 to get the thread, he did not know the first thread to get a few of num
# Similarly when the time.sleep second thread, a thread switch, or the third to get 100

5.2, problem solving

Guess you like

Origin www.cnblogs.com/hujinzhong/p/11517535.html