Python standard library 08 multithreading and synchronization (threading package) Linux multithreading and synchronization Python dynamic type python basic concepts of object-oriented Python Object Oriented further expansion of Linux

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Python mainly usesThreadingPackage implementationMultithreading. In today'sNetworkEvery server receives a large number of requests. The server can process these requests in multiple threads to improve the read/write efficiency of Network Ports. Python is a background working language for network servers (such as Douban.com), so multithreading is naturally supported by python.

(For the principles of multithreading and the C implementation method, refer to the Linux multithreading and synchronization I wrote earlier. To understand the concepts of race condition, mutex, and condition variable)

 

1. multi-thread ticketing and Synchronization

We use python to implement multi-thread Linux and ticket sales in synchronization.Program. We use mutex (that is,LockClass Object) To achieve thread synchronization:

 #  A program to simulate selling tickets in multi-thread way  #  Written by vamei  Import  Threading  Import  Time  Import  OS  # This function cocould be any function to do other chores.  Def  Dochore (): Time. Sleep ( 0.5 )  #  Function for each thread  Def  Booth (TID ):  Global  I  Global  Lock
While True: Lock. Acquire ()#Lock; or wait if other thread is holding the lock If I! = 0: I = I-1 # Effectickets
Print (TID, ' : Now left: ' , I) # Tickets left Dochore () # Other critical operations Else : Print ( " Thread_id " , Tid, " No more tickets " ) OS. _ exit (0) # Exit the whole process immediately Lock. Release ()#UnblockDochore ()#Non-critical operations # Start of the Main Function I = 100. # Available ticket number Lock = threading. Lock () # Lock (I. e., mutex) # Start 10 threads For K In Range (10 ): New_thread = Threading. Thread (target = booth, argS = (K ,))#Set up thread; Target: The callable (function) to be run, argS: the argument for the callableNew_thread.start () # Run the thread

We use two global variables, I, to store the remaining number of votes, and lock object, to synchronize thread changes to I. In addition, we set a total of 10 threads in the final for loop. Each thread executes the booth () function. The thread is officially started when the START () method is called (in fact, there are a maximum of 11 threads in the computer, because the main program also occupies one thread ). Use PythonThreading. ThreadObject To represent the thread, usingThreading. LockObject To represent a mutex ).

Note the following two points:

    • We useGlobalTo declare variables as global variables, so that multithreading can share I and lock (in C, we place variables outside all functions to make them global variables ). If not, because I and lock areImmutable data object, Which will be treated as a local variable (see Python dynamic type ). If yesVariable data objectThe global statement is not required. We can evenVariable data objectPassed to the thread function as a parameter. These threads will share these variable data objects.
    • We used twoDochore ()Function. Can improve the program in the future, so that the thread in addition to the I = I-1, do more operations, such as printing the remaining number of votes, looking for money, or drink saliva and so on. The first dochore () is still inside the lock, so it can be safelyUse shared resources(Critical operations, such as printing the remaining number of votes ). The second dochore (), lock has been released, so you can no longer use shared resources. At this time, you can do someDo not use shared resourcesNon-critical operation (for example, seeking for money or drinking water ). I intentionally asked dochore () to wait 0.5 seconds to indicate the time that these additional operations may take. You can define a function to replace dochore ().

 

2. Oop creation thread

The above Python program is very similar to a process-oriented C program. The following describes how to useObject-oriented(OOP, object-oriented programming, refer to the basic concepts of Python object-oriented and the further expansion of Python object-oriented ).Threading. ThreadClass. We have used threading in the for loop above. thread () method to create a thread object, pass the function Booth () and its parameters to the modified object, and call the START () method to run the thread. By modifying the Thread classRun ()Method to define the command to be executed by the thread.

 #  A program to simulate selling tickets in multi-thread way  #  Written by vamei  Import  Threading  Import Time  Import  OS  #  This function cocould be any function to do other chores.  Def  Dochore (): Time. Sleep ( 0.5 )  #  Function for each thread  Class  Booththread (threading. Thread ):  Def   _ Init __ (Self, tid, monitor): Self. TID = TID self. Monitor = Monitor
Threading. thread. _ init _ (Self) Def Run (Self ): While True: monitor [ ' Lock ' ]. Acquire () # Lock; or wait if other thread is holding the lock If Monitor [ ' Tick ' ]! = 0: monitor [ ' Tick ' ] = Monitor [ ' Tick ' ]-1 # Effectickets
Print (Self. TID,' : Now left: ' , Monitor [ ' Tick ' ]) # Tickets left Dochore () # Other critical operations Else : Print ( " Thread_id " , Self. TID, " No more tickets " ) OS. _ exit (0) # Exit the whole process immediately Monitor [ ' Lock ' ]. Release () # Unblock Dochore () # Non-critical operations # Start of the Main Function Monitor = { ' Tick ' 100, ' Lock ' : Threading. Lock ()} # Start 10 threads For KIn Range (10 ): New_thread = Booththread (K, monitor) new_thread.start ()

We have defined a class.Booththread, This classInherited from thread. Threading class. Then we put all the operations performed by Booth () above into the booththread classRun ()Method. Note that we useDictionaryMonitor stores global variables and passes the dictionary as a parameter to the thread function. Because the dictionary isVariable data objectSo when it is passed to the function, the function still uses the same object, which is equivalent to being shared by multiple threads. This is also a technique of multithreading and multi-process programming (we should try to avoid the usage of the above global declaration because it is not applicable to Windows platforms ).

Compared with the process-oriented programming method, the above OOP programming method does not bring about much substantial difference.

 

3. Others:

Threading. ThreadObject: we have introduced the START () and run () of this object. In addition:

    • Join ()Method, the thread that calls this method will wait until the thread object is modified and then resume running. This is similar to calling the wait () function between processes.

 

The following object is used for processingMulti-thread synchronization. Once an object is created, it can be shared by multiple threads and some processes are blocked as needed. Please refer to the synchronization tool in Linux multithreading and synchronization.

Threading. LockObject: mutex, which has the acquire () and release () methods.

Threading. ConditionObject: Condition variable. When this object is created, it contains a Lock Object (because condition variable is always used together with mutex ). You can call the acquire () and release () Methods on the condition object to control the potential lock object. In addition:

    • Wait ()Method, equivalent to cond_wait ()
    • Policy_all (), Equivalent to cond_broadcast ()
    • Nofify (), Similar to the notify_all () function, but only wakes up one waiting thread, not all
Threading. semaphoreObject: semaphore, that is, the Count lock (semaphore is traditionally a tool for inter-process synchronization. For details, refer to inter-process communication in Linux ). When creating an object, you can pass an integerMaximum count(SEMA = threading. semaphore (5 )). It is similar to lock, and there are also two lock methods.
Threading. EventObject: similar to threading. condition, it is equivalent to a condition variable without potential lock protection. The object has two statuses: true and false. Multiple Threads can use wait () to wait until a thread callsSet ()Method To set the object to true. The thread can call the object'sClear ()Method to reset the object to the false state.
 
 
4. exercise:
Reference Linux multithreading and SynchronizationThe example of condition variable in is implemented in Python. We also consider using process-oriented and object-oriented programming methods.
For more details about threading, refer:

Http://docs.python.org/library/threading.html

 

Summary:

Threading. Thread

Lock, condition, semaphore, event

Related 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.