Python standard library 08 multi-Threading and Synchronization (threading package)

Source: Internet
Author: User
Tags mutex semaphore

Python implements multithreading primarily through the threading package in the standard library. In today's network era, each server receives a large number of requests. The server can handle these requests in a multi-threaded manner to improve the read and write efficiency of the network ports. Python is a Web server's background working language (such as Douban), so multithreading is naturally supported by the Python language.

(For the principle of multithreading and C implementation method, please refer to my previous written Linux multi-threading and synchronization, to understand race condition, mutexes and condition variable concept)

Multi-threaded ticketing and synchronization

We use Python to implement the ticketing procedures in the Linux multi-threading and synchronization text. We use a mutex (that is, the lock class object in Python) to implement thread synchronization:

# Simulate selling tickets in Multi-thread way# written by Vameiimport Threadingimport Timeimport os# This fu     Nction could is any function to does other Chores.def Dochore (): Time.sleep (0.5) # function for each threaddef 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 # Sell Tickets
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 ()             &NBSP ;  # Unblock Dochore ()                    # non-critical Oper ations# Start of the main functioni = # Available Ticket Number lock = Threading. Lock () # lock (i.e., mutex) # Start Threadsfor K in range: New_thread = Threading. Thread (target=booth,args= (k,))   # Set up thread; Target:the callable (function) to is run, args:the argument for the callable New_thread.start ()                         # run the thread

We used two global variables, one for I, to store the remaining votes, and one for the lock object to synchronize the thread-to-I modifications. In addition, in the final for loop, we set up a total of 10 threads. Each thread executes the booth () function. The thread is formally started when the start () method is called (in fact, there are up to 11 threads in the computer, because the main program itself consumes one thread). Python uses the Threading.thread object to represent the thread and uses the Threading.lock object to represent a mutex (mutex).

There are two points to note:

    • We use global in our functions to declare variables as global variables, allowing multithreading to share I and lock (in C, we make it a global variable by placing variables out of all functions). If you do not declare this, because I and lock are immutable data objects, they will be treated as a local variable (see Python dynamic type). If it is a mutable data object, then the global declaration is not required. We can even pass a mutable data object as a parameter to the thread function. These threads will share these mutable data objects.
    • We used two dochore () functions in booth. The program can be improved in the future to allow threads to do more than i=i-1, such as printing the remaining votes, making money, or drinking saliva. The first Dochore () is still inside lock, so it is safe to use shared resources (critical operations, such as printing the remaining votes). At the second dochore (), lock has been released, so it is no longer possible to use a shared resource. This is where you can do things that don't use shared resources (Non-critical operation, like change, water). I deliberately let dochore () wait 0.5 seconds to represent the amount of time that these extra operations might take. You can define the function to replace Dochore ().

OOP Creating Threads

The Python program above is very similar to a process-oriented C program. Here we describe how to implement multi-threading through object-oriented (OOP, object-oriented programming, the basic concept of Python object-oriented, and the further development of Python's object-oriented approach), with the core of inheriting threading. Thread class. We have used threading in the for loop above. The thread () method creates a thread object and passes the function booth () and its arguments to the modified object, and calls the start () method to run the thread. OOP, you define the command that the thread executes by modifying the run () method of the thread class.

# Simulate selling tickets in Multi-thread way# written by Vameiimport Threadingimport Timeimport os# This fu Nction could be any function to does other Chores.def Dochore (): Time.sleep (0.5) # function for each threadclass Booththre AD (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 # Sell Tickets
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 operation s# Start of the main functionmonitor = {' Tick ': +, ' Lock ': Threading. Lock ()}# Start threadsfor K in range (Ten): New_thread = Booththread (k, monitor) New_thread.start ()

We define a class booththread ourselves, which inherits from thread. Threading class. We then put the actions of the above booth () into the run () method of the Booththread class. Note that instead of using global variables to declare the global variable, we use a dictionary monitor to store the globals and then pass the dictionary as a parameter to the thread function. Since the dictionary is a mutable data object, when it is passed to the function, the function is still using the same object, which is equivalent to being shared by multiple threads. This is also a technique for multithreading and even multi-process programming (you should try to avoid the use of the global declaration above as it does not apply to the Windows platform).

There is no significant difference between OOP programming methods and process-oriented programming methods.

Other

Threading. Thread object: We have introduced the object's start () and run (), in addition:

    • Join () method, the thread that calls the method waits until it is finished with the thread object, and then resumes running. This is similar to calling the wait () function between processes.

The following objects are used to handle multi-threaded synchronization. Once an object is created, it can be shared by multiple threads and block certain processes as appropriate. Please read with the Sync Tool reference in Linux multi-threading and synchronization.

Threading. Lock object: Mutex, with Acquire () and release () methods.

Threading. Condition object: Condition variable, when the object is created, it contains a lock object (because condition variable is always used with the 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 ()
    • Notify_all (), quite with cond_broadcast ()
    • Nofify (), similar to the Notify_all () feature, but only wakes up a waiting thread, not all
Threading. Semaphore object: Semaphore, which is the count lock (semaphore is traditionally an inter-process synchronization tool, see inter-process communication between Linux). When creating an object, you can pass an integer as the upper limit of the count (Sema = threading. Semaphore (5)). It is similar to lock and has two methods for lock.
Threading. Event object: With Threading. The condition is similar to the condition variable without the potential lock protection. The object has a status of true and false two. You can use Wait () waits for multiple threads until a thread calls the object's set () method and sets the object to True. A thread can call the object's clear () method to reset the object to a false state.
Practice
Refer to the example of condition variable in Linux multi-threading and synchronization, using Python implementations. Consider using both process-oriented and object-oriented programming methods.
For more information on threading, please refer to:

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

Summarize

Threading. Thread

Lock, Condition, Semaphore, Event

Python standard library 08 multi-Threading and Synchronization (threading package)

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.