python basis of multi-threaded daemon thread threading join setDeamon recursive lock Rlock

It begins with a brief overview of the difference between multi-threaded and multi-process, a detailed theoretical difference they can look at other blog search, not repeat them here

Shared memory data under multiple threads of the same process, there is no relationship between the primary and secondary multiple threads can operate with each other; cpu threads are executed, the default program will open a main thread; processes and procedures are procedures and related resources collection; some scenarios we can use multiple threads to achieve the purpose of improving the efficiency of the program, following a brief explanation on some of the basics of multithreading

Simple multi-threaded

. 1  Import Threading, Time
 2  
. 3  DEF test1 (X):
 . 4      the time.sleep (. 5 )
 . 5      Print (X ** X)
 . 6  
. 7  # defined below test1 two threads call this function, using the following syntax to create a multi-threaded, target back with the function name, args argument passing, need to pass arguments in the form of tuples 
. 8 START_TIME = the time.time ()
 . 9 T1 of the threading.Thread = (target = test1, args = (. 5 ,))
 10 T2 of the threading.Thread = ( = test1 target, args = (. 6 ,))
 . 11  # start multithreaded 
12 is  t1.start ()
 13 is  t2.start ()
 14 END_TIME = the time.time ()
 15= end_time TOTAL_TIME - start_time
 16  Print ( " TWO% S the Thread Used Time " % TOTAL_TIME) # the use of multi-threading, t1 t2 after the waiting period does not start executing the program before continuing to go backwards, because the main program and is the main thread t1 t2 are executed in parallel, the main program to perform this operation did not complete t1 t2 
17  
18 the time.sleep (6 )
 19  # result of multiple thread starts comparing the number can be used for a long time loop, multi-threaded parallel execution, there may not be printed the startup sequence to be printed 
20 is  for I in Range (. 5 ):
 21 is      T3 of the threading.Thread = (target = test1, args = (I,))
 22 is      t3.start ()
 23 is the time.sleep (. 6)

The main thread waits for a non-main thread is finished before continuing execution join method

1  # some cases require the child thread executed the main thread After completion, there may be something to be executed before the next primary thread data processed 
2 START_TIME1 = the time.time ()
 . 3 TL = [] # object stored multithreading up to behind the join method 
. 4  for I in Range (. 5 ):
 . 5      T4 of the threading.Thread = (target = test1, args = (I,))
 . 6      t4.start ()
 . 7      tl.append (T4)
 . 8  for T in TL: # the multi-threaded join, join the sub-thread execution to attend after completion to continue following the main thread. 
. 9      t.join ()
 10 END_TIME1 = the time.time ()
 . 11 total_time1 = END_TIME1 - START_TIME1
12  Print (total_time1) # The execution time is about 5s

 

1  # how to deal with if multiple child threads that have no join join some of the main thread? ? ? Join after the end of the longest part of the child thread child thread join the main thread will wait to continue, not involved in the child thread and still join the main threads running in parallel 
2 T5 = threading.Thread (target = test1, args = (5 ,))
 . 3 T6 of the threading.Thread = (target = test1, args = (. 6 ,))
 . 4  t5.start ()
 . 5  t6.start ()
 . 6 t5_join_start_time = the time.time ()
 . 7  t5.join ()
 . 8 the time.sleep (10 )
 . 9 t5_join_end_time = the time.time ()
 10  Print ( " T5 the Join Time% S IS " % (t5_join_end_time - t5_join_start_time)) # actual consuming 15s

 

Daemon thread setDeamon

1  # daemon, that is, after the end of the main thread of all the other threads also ended immediately, without waiting for the other thread is finished; normally even without adding join the main thread is finished when the other threads to complete the unfinished program does not exit, you must wait for all the thread is finished before the end of the program, similar to the main program at the end there is a default join 
2  DEF test1 (the X-):
 3      the time.sleep (5 )
 4      Print ( " i AN OTHER the thread " , the X-** the X-)
 5  
6  for i in Range (. 5 ):
 . 7      T of the threading.Thread = (target = test1, args = (I,))
 . 8      t.setDaemon (True)
 . 9      t.start ()
 10  
. 11  Print ( " the Main the Thread IS DONE " ) #The entire program ends, does not wait for a daemon thread printing operations are completed directly ended

Recursive lock Rlock

1  # recursive lock, a lock nested inside the lock, the lock if you do not use recursion can lead to release the lock logic errors, the entire program went wide; the use of recursive lock program maintains a lock to unlock the data structure, to ensure the lock is released no problem 
2 Lock = of threading.Lock ()
 . 3  DEF test2 ():
 . 4      lock.acquire ()
 . 5      Print ( " the this IS test2 " )
 . 6      lock.release ()
 . 7  
. 8  DEF Test3 ():
 . 9      lock.acquire ()
 10      Print ( " the this Test3 IS " )
 . 11      lock.release ()
 12 is  
13 is  DEF Test4 ():
 14      lock.acquire ()
 15     test2 ()
 16      Print ( " the this Test4 IS " )
 . 17      Test3 ()
 18 is      lock.release ()
 . 19  
20 is rlock_test of the threading.Thread = (target = Test4)
 21 is  rlock_test.start ()
 22 is  
23 is  the while threading.active_count ()! = 1 :
 24-      Print ( " Current COUNT iS the thread " , threading.active_count ()) # entire program has been printed with two threads, the main thread of the non-nesting lock problem cause can not exit the entire program stuck 
25      the time.sleep (1)

The lock = threading.Lock () amended to lock = threading.RLock () the entire program can be ended normally; normal end output follows

this is test2
this is test4
current thread count is 2
this is test3

 

Guess you like

Origin www.cnblogs.com/flags-blog/p/12443307.html