Python: Advanced thread and advanced python thread

Source: Internet
Author: User

Python: Advanced thread and advanced python thread

1. daemon thread

Import timefrom threading import Threaddef func (): print ('start executing subthreads') time. sleep (3) print ('subthread execution completed ') t = Thread (target = func) t. setDaemon (True) # Set daemon as a daemon attribute = Truet. start () t2 = Thread (target = func) t2.start () t2.join () # Wait Until t2 ends
# The daemon process waits for the code in the main process or main thread to be executed.
# T2 = Thread (target = func)
# T2.start () ---> code execution is complete
# The daemon thread ends.
# The main thread is not finished yet. Wait for T2.
# T2 execution completed main thread ended

# T2 = Thread (target = func)
# T2.start ()
# T2.join () # The code is executed only after T2.
# T2 thread execution completed
# No code in the main thread. The daemon thread ends.

2. Lock

Import timefrom threading import Threadfrom threading import Lockdef func (): global n time. sleep (2) lock. acquire () temp = n # obtain n time from the process. sleep (0.01) n = temp-1 # Get the result and store it back to the process lock. release () n = 100 lock = Lock () t_lst = [] for I in range (100): t = Thread (target = func) t. start () t_lst.append (t) [t. join () for t in t_lst] print (n) # GIL does not lock data but lock threads # in special cases of multithreading, it is still necessary to lock the data

3. deadlock

Import timefrom threading import RLockfrom threading import Threadm = kz = RLock () # def eat (name): kz. acquire () # Get key print ('% s get chopsticks' % name) m. acquire () print ('% s got jackfruit dried' % name) print ('% s eat jackfruit dried' % name) m. release () kz. release () def eat2 (name): m. acquire () # No key print ('% s got jackfruit dried' % name) time. sleep (1) kz. acquire () print ('% s got chopsticks' % name) print ('% s eat jackfruit dried' % name) kz. release () m. release ()
Thread (target = eat, args = ('rabbit ',). start ()
Thread (target = eat2, args = ('rabbit ',). start ()
Thread (target = eat, args = ('Big orange ',). start ()
Thread (target = eat2, args = ('Orange _ cat',). start ()
 
In different threads, the operation on the two data occurs, so a deadlock occurs.
4. semaphores
1 import time 2 import random 3 from threading import Thread 4 from threading import Semaphore 5 def func (n, sem): 6 sem. acquire () 7 print ('thread-% s start' % n) 8 time. sleep (random. random () 9 print ('thread-% s done' % n) 10 sem. release () 11 sem = Semaphore (5) # A lock has 5 Keys 12 for I in range (20): 13 Thread (target = func, args = (I, sem )). start () 14 # What is the difference between a semaphore and a thread pool? 15 # similarities after the semaphore acquire, same as the thread pool, only n 16 threads can be executed at the same time. # difference 17 # The number of opened threads is different. In the thread pool, only five threads are opened. semaphores have several tasks, and several threads are opened. 18 # Can programs with semaphore restrictions execute multiple threads at the same time? 19 # In fact, semaphores do not affect the thread or process concurrency, but only limit the traffic during the lock phase.

5. Events

Import timeimport randomfrom threading import Eventfrom threading import Threaddef conn_mysql (): # connect to the database count = 1 while not e. is_set (): # if count> 3: raise TimeoutError print ('attempt to connect % s' Times '% count) is executed only when the flag of the event is False) count + = 1 e. wait (0.5) # always blocking turns to blocking only 0.5 print ('Connection successfully') # receives the set command in the check_conn function, and changes the flag to True to jump out of the while loop, run the following code def check_conn (): ''' to check whether the database server connection is normal ''' time. sleep (random. randint (1, 2) # simulate the connection detection time e. set () # indicates the identifier of the Event. The database can connect to e = Event () check = Thread (target = check_conn) check. start () conn = Thread (target = conn_mysql) conn. start ()

6. Conditions

Import threadingdef run (n): con. acquire () con. wait () # wait for print ("run the thread: % s" % n) con. release () if _ name _ = '_ main _': con = threading. condition () # Condition = lock + wait function for I in range (10): t = threading. thread (target = run, args = (I,) t. start () while True: indium = input ('>>>') if indium = 'q': break con. acquire () # The locks in condition are recursive locks. if indium = 'all': con. policy_all () else: con. Y (int (indium) # transmit signal notify (1) --> A thread con can be opened. release ()

7. Timer

From threading import Timerdef hello (): print ("hello, world") while True: # Start a thread t = Timer (10, hello) at intervals) # enable a thread regularly and execute a task # timing: after how long is the unit of s # the task to be executed: function name t. start ()

8. Queue

Import queuepq = queue. priorityQueue () # The smaller the value, the higher the priority. If the value is the same, the first-out pq with a small asc code. put (1, 'z') pq. put (1, 'B') pq. put (15, 'C') pq. put (2, 'D') # print (pq. get () print (pq. get ())

9. concurrent and thread pool and callback Functions

Import timeimport randomfrom concurrent import futuresdef funcname (n): print (n) time. sleep (random. randint (1, 3) return n * 'def call (args): print (args. result () thread_pool = futures. threadPoolExecutor (5) # thread_pool.map (funcname, range (10) # map, which is naturally asynchronous and receives data of iteratable objects, the return value f_lst = [] for I in range (10): f = thread_pool.submit (funcname, I) # submit combines the function of creating thread objects and starting f_lst.append (f) thread_pool.shutdown () # close () join () for f in f_lst: # print (f. result () # f. result () blocking and other f execution results # callback function add_done_callback (callback function name) thread_pool.submit (funcname, 1 ). add_done_callback (call)

 



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.