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

Source: Internet
Author: User

Vamei Source: Http:// Welcome reprint, Please also keep this statement. Thank you!

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:

#A program to simulate selling tickets in Multi-thread#Written by VameiImportThreadingImportTimeImportOs#This function could is any function to does other chores.DefDochore (): Time.sleep (0.5)#Function for each threadDefBooth (TID):GlobalIGlobalLockWhileTrue:lock.acquire ()#Lock; Or wait if other thread is holding the lockIf I! =0:I = i-1#Sell TicketsPrint (TID,‘: Now Left:', i)#Tickets left Dochore ()#Other critical operationsElse:Print"thread_id", Tid,"No More Tickets") os._exit (0)#Exit the whole process immediatelyLock.release ()#Unblock Dochore ()#Non-critical operations# Start of the main functioni = # Available ticket Number lock = Threading. Lock () # lock (i.e., mutex) # Start threadsfor K Span style= "color: #0000ff;" >in Range (10 (K,))   # Set up thread; Target:the callable (function) to is run, args:the argument for the callable New_thread.start ()       &NB Sp                 # 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 threading. The thread object is used to represent threads, using threading. The lock object represents 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 object-oriented), 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.

#A program to simulate selling tickets in Multi-thread#Written by VameiImportThreadingImportTimeImportOs#This function could is any function to does other chores.DefDochore (): Time.sleep (0.5)#Function for each threadClassBooththread (Threading. Thread):Def__init__(Self, Tid, monitor): Self.tid =Tid self.monitor =Monitor Threading. Thread.__init__ (self)DefRun (self):Whiletrue:monitor[‘Lock'].acquire ()#Lock; Or wait if other thread is holding the lockIf monitor[‘Tick']! =0:monitor[‘Tick'] = monitor[‘Tick']-1#Sell TicketsPrint (Self.tid,‘: Now Left:', monitor[‘Tick‘])#Tickets left Dochore ()#Other critical operationsElse: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 functionmonitor = {'  Tick': +, 'lock': Threading. Lock ()}# Start threads forK in range: New_thread = Booththread (k, monitor) New_threa D.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.


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



Threading. Thread

Lock, Condition, Semaphore, Event

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

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