Threads and Multithreading Advanced Chapter

Source: Internet
Author: User

python--Threading and multithreading advanced

Before we learned how to create a new thread in a block of code to perform multiple tasks that we want to perform synchronously, the world of threads is much more than that. Next, we're going to cover the entire threading module. Threading Java-based threading model design. Lock and condition variables (Condition) are the basic behavior of the object in Java (each object has its own lock and condition variable), and in Python it is a separate object, so the Python threading module also provides Lock,rlock, Common classes such as Condition,event, which are independent of the tread module in Python, are closely related to threads and are inseparable.

It is important to note thatthere is no priority, thread group, or stop, pause, resume, or interrupt in the Python thread , and the thread can only be destroyed as the code in the thread finishes executing. After checking the N-multi-data, I finally accepted the above facts, personally think this is a python pit, causing me to implement the thread pool can not stop the method has been injected and the execution time-out of the threads.

Classes provided by the threading module:
Thread, Lock, Rlock, Condition, [Bounded]semaphore, Event, Timer, Local.

common methods provided by the threading module:
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 ()).

Threading module Common Class

Thread class: We use the thread class to create a new thread

    • Start thread is ready to wait for CPU scheduling
    • SetName setting a name for a thread
    • GetName Get thread Name
    • Setdaemon set to background thread or foreground thread (default)
      If it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread finishes executing, the background thread stops regardless of success or not.
      If it is the foreground thread, during the main thread execution, the foreground thread is also in progress, and after the main thread finishes executing, wait for the foreground thread to finish, the program stops
    • The join executes each thread one by one and continues execution after execution, making multithreading meaningless
    • Run method that executes the thread class object after the run thread is dispatched by the CPU

Lock class and Rlock class: Due to random scheduling between threads: A thread might execute another thread after n is executed. In order for multiple threads to operate an in-memory resource at the same time without confusion, we use the lock

      • Acquire to Thread lock
      • Release To line Threads unlocked

Whether it's lock or Rlock, the methods offered are very simple, acquire and release. But what is the difference between Rlock and lock? Rlock is allowed to be acquire multiple times in the same thread. But lock does not allow this situation. Note: If you use Rlock, then acquire and release must appear in pairs, that is, n times acquire is called, and the N-time release must be called to actually release the lock that is occupied.

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jason WangImportThreading Lock= Threading. Lock ()#Lock ObjectLock.acquire () lock.acquire ( )#A deadlock has been created. lock.release () lock.release ( )ImportThreading Rlock= Threading. Rlock ()#Rlock ObjectRlock.acquire () rlock.acquire ( )#within the same thread, the program does not clog. rlock.release () rlock.release ( )

Condition class: A conditional variable object allows a thread to stop waiting for another thread to satisfy a certain "condition." For example, change of state or change in value.

    • Acquire to Thread lock
    • The wait wait method frees the lock occupied by the current thread while suspending the thread until it wakes up or times out (the timeout parameter is required). The program will continue to execute when the thread is awakened and the lock is re-occupied.
    • Notify wakes up a suspended thread (if there is a pending thread). Note: the Notify () method does not release the locks that are occupied.
    • Notifyall calls this method to notify all threads in the pool to wait for the lock pool to attempt to obtain a lock. This method does not release the lock. The use of the front thread must have been locked or an exception will be thrown.

The classic example is the following example of the producer and consumer, this example of the online search is everywhere, here is a simple explanation of the meaning of this code, the code wrote two classes, consumer and producer, respectively, inherited the thread class, We initialize each of the two classes to get the C and P objects, and start the two threads. Then these two threads execute the Run method (which is related to scheduling inside the thread Class), define the producer global variable and the condition object as a global variable, and when producer is no more than 1 o'clock, the consumer thread is blocked by the condition object, Can not continue to consume (here is no longer diminishing), when the producer is not less than 10 o'clock, the producer thread is blocked by the condition object, no longer production (here is no longer cumulative), the code is below, take to execute, breakpoint a bit to understand.

ImportThreadingImporttimecondition=Threading. Condition () Products=0classProducer (Threading. Thread):def __init__(self): threading. Thread.__init__(self)defRun (self):Globalcondition, products whileTrue:ifCondition.acquire ():ifProducts < 10: Products+ = 1; Print("Producer (%s):d Eliver One, now products:%s"%(Self.name, Products)) condition.notify ()Else:                    Print("Producer (%s): Already, stop deliver, now products:%s"%(Self.name, Products)) condition.wait (); Condition.release () Time.sleep (2)classConsumer (Threading. Thread):def __init__(self): threading. Thread.__init__(self)defRun (self):Globalcondition, products whileTrue:ifCondition.acquire ():ifProducts > 1: Products-= 1Print("Consumer (%s): Consume one, now products:%s"%(Self.name, Products)) condition.notify ()Else:                    Print("Consumer (%s): Only 1, stop consume, products:%s"%(Self.name, Products)) condition.wait (); Condition.release () Time.sleep (2)if __name__=="__main__":     forPinchRange (0, 2): P=Producer () P.start ( ) forCinchRange (0, 10): C=Consumer () C.start ( )
Condition Code

Event class: A generic condition variable. Multiple threads can wait for an event to occur, and all threads will be activated after the event.

    • Event.wait (timeout) when flag is ' False ', the thread will be blocked
    • Clear set "Flag" to False
    • Set "Flag" to True
    • Is_set returns the current ' Flag '

This is a more critical class, and I saw the Python threadpool module used when I wrote the thread pool. The point is that you can control multiple instanced objects that belong to the same thread class and let them block or execute at the same time. Matching queues to implement a thread pool is very useful. In the next blog, we will continue to introduce. Let's start with a small example of practicing practiced hand here and learn about the usage of the event.

#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jason WangImportThreadingdefDo (event):Print('Start') event.wait ()#execute the Object Weit method, and then they stop and wait for "Flag" to True    Print('Execute') Event_obj= Threading. Event ()#object that created the event forIinchRange (3): T= Threading. Thread (Target=do, args= (Event_obj,))#the object goes inside each thread ~T.start () event_obj.clear ( )#set "Flag" to FlaseINP= Input ('Input:')ifINP = ='true': Event_obj.set ()#thread enent is the use of this 3 method##output#Start#Start#Start#input:true#Execute#Execute#Execute
Thread Event Code

Reference documents:

  Python Multithreaded Learning Summary: http://www.myexception.cn/perl-python/1688021.html

Python Threading Guide: http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html

Threading. Rlock and Threading.lock:http://blog.sina.com.cn/s/blog_5dd2af0901012rad.html

Python process, thread, and coprocessor: http://www.cnblogs.com/wupeiqi/articles/5040827.html

Threads and multithreading advanced article

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.