Python study day 46 (genlock)

  The first is to continue today looked at the issue of GIL, in fact, than the problem itself after yesterday verified, there is a more alarming question, why listen and game after using multithreading time shortened, but after add and cheng with the multi-threaded computation time increase but instead, here I come slowly Syria:

  The first is the task we often deal with classified into two types:

    1.IO intensive (large number of data entry)

    2. compute-intensive (very high end of the underlying computing, or like the kind of data discrepancies appear yesterday)

  Special instructions, time.sleep () is a similar wait for the data entry process, which is a reasonable explanation of why the game is to save time and listen, the general situation in the following figure:

 

   Can be found when running listen to wait 3 seconds, multi-thread arranged for running game, and the game content to wait for 3 seconds 5 seconds during the entire execution portion listen put over, the handover execution time is much less than io latency input, so the fact is that less time waiting thread io, that would save time.

  Then the problem and add cheng, both basically no wait time io operations, is a pile of pure calculation, but the interpreter also kept switching the two threads, so waste a lot of time, leading to the old a real honest to a complete recalculation calculated by the following more time-saving.

  So Python language is better suited to operate io-intensive tasks, in fact, in most instances life is io-intensive, but also the nature of a problem, there is no way I played the strong performance of quad-core computer, how this to achieve it, continue to look at the chart yesterday, I added some stuff.

 

   In fact, GIL here is to set up a lock for each process, and more so if we establish a process to execute the two methods, respectively, it is not on the Well OK, so both cores simultaneously start the task.

  However, this method is recommended to use a master, each additional process memory consumption is quite large, so use it with caution and tutorials in the end did not tell me how to use.

 

  Summary: In the course of time multithreading io processing-intensive tasks, the cpu is to wait for the user to input data, arranged to execute another thread, etc. after you complete the entry to return to continue to improve the efficiency of cpu

  Then referred to a concept: multi-threaded + coroutine

Here is the second part:

  It says clearly the working mechanism of multi-threaded, the following error because such a multi-threaded cause occurs:

  Requirements, the use of multi-threading implementation process of a decreasing.

  Look at the code that runs the correct form:  

import time
import threading

def addNum():
    , Ltd. Free Join NUM # get all the global variables in each thread
    a - = 1

NUM = 100   # to set a shared variable
thread_list = []
for i in range(100):
    t = threading.Thread(target=addNum)
    t.start()
    thread_list.append(t)

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

print ( ' end in: ' , a)

   At this time, the final output is 0 num

  Then we modify the form addNum, then the assignment operation in two steps and, at the same time break of 0.01 seconds

import time
import threading

DEF addNum ():
     , Ltd. Free Join NUM # will get this global variables in each thread 
    the TEMP = NUM
    time.sleep(0.1)
    NUM = TEMP. 1- # this public variables 1 operation 

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

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

print ( ' end in: ' , a)

  At this point then you will find that the value of num will happen with the change in the length of time.sleep () time

  Small figure when the thief still has a chance equal to 0, but in most cases a digital still become greater than 0

  General running process

 

  The reason is very obvious, at first descending into the waiting time, other threads running, cause there may be multiple temp is assigned the value 100, which ultimately led to the deviation calculation results, mainly before we did not finish running in a thread were switching between threads, then in order to ensure that this will not be switched, we introduced a genlock

 

  Genlock: Genlock within the program does not run to completion, determined not to switch

  After modification to the perfect solution to the above problem

import time
import threading

Lock = threading.Lock ()
 DEF addNum ():
     , Ltd. Free Join NUM # will get this global variables in each thread 
    lock.acquire ()    # locked 
    the TEMP = NUM
    time.sleep(0.1)
    NUM = TEMP. 1- # this public variables 1 operation 
    lock.release ()    # unlocking 

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

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

print ( ' end in: ' , a)

  By increasing lock.acquire () and lock.release () to ensure that the switching code running in the middle of their threads do not happen, so that we can guarantee a perfect one by one to run the program.

 

  Recent theoretical knowledge of a lot of ah, then go to work a lot of good hybrid, Naogua Zi hurt, taking the time to get reviewed, as well as in front put a lot of satellites, has not been done, I feel now forgotten clean, good night friends. . .

 

Guess you like

Origin www.cnblogs.com/xiaoyaotx/p/12664077.html