Python3.4 multiple threads implement synchronization in four ways (lock mechanism, conditional variable, semaphore and synchronization Queue)

Source: Internet
Author: User

Python3.4 multiple threads implement synchronization in four ways (lock mechanism, conditional variable, semaphore and synchronization Queue)

Critical resources are the resources that can only be accessed by one thread at a time. A typical example is a printer. It can only be used by one program to execute the printing function at a time, because it cannot be operated simultaneously by multiple threads, the code used to access these resources is usually called a critical section.

The Lock class of threading. Use the acquire function of this class to Lock and use the realbench function to unlock the Lock.

 

Import threadingimport timeclass Num: def _ init _ (self): self. num = 0 self. lock = threading. lock () def add (self): self. lock. acquire () # Lock and lock the corresponding resource self. num + = 1 num = self. num self. lock. release () # unlock and exit the resource return numn = Num () class jdThread (threading. thread): def _ init _ (self, item): threading. thread. _ init _ (self) self. item = item def run (self): time. sleep (2) value = n. add () # add num to 1 and output the original data and print (self. item, value) for item in range (100): t = jdThread (item) t. start () t. join () # Run threads one by one

 

 

Semaphores also provide the acquire and release methods. When the acquire method is called, if the internal counter is greater than 0, it is reduced by 1. If the internal counter is equal to 0, the thread is blocked, it is known that a thread has called the release method to update the internal counter to a position greater than 1.

 

Import threadingimport timeclass Num: def _ init _ (self): self. num = 0 self. sem = threading. semaphore (value = 3) # allows up to three threads to simultaneously access the resource def add (self): self. sem. acquire () # The internal counter minus 1 self. num + = 1 num = self. num self. sem. release () # Add 1 return num n = Num () class jdThread (threading. thread): def _ init _ (self, item): threading. thread. _ init _ (self) self. item = item def run (self): time. sleep (2) value = n. add () print (self. item, value) for item in range (100): t = jdThread (item) t. start () t. join ()

The so-called condition variable, that is, this mechanism can be used by the thread to access relevant data only after the specific conditions are met.

 

It uses the Condition class, because it can also be used as the lock mechanism, so it also has the acquire method and the release method, and it also has the wait, policy, policyall method.

 

"A simple production consumer model controls the increase or decrease of the number of products by using the conditional variable. One call of the producer product is + 1, and one call of the consumer product is-1. "uses the Condition class, because it can also be used as the lock mechanism, so it also has the acquire method and the release method, and it also has wait, policy, policyall method. "Import threadingimport queue, time, randomclass Goods: # product class def _ init _ (self): self. count = 0 def add (self, num = 1): self. count + = num def sub (self): if self. count> = 0: self. count-= 1 def empty (self): return self. count <= 0 class Producer (threading. thread): # producer class def _ init _ (self, condition, goods, sleeptime = 1): # sleeptime = 1 threading. thread. _ init _ (self) self. cond = condition self. goods = goods self. sleeptime = sleeptime def run (self): cond = self. cond goods = self. goods while True: cond. acquire () # Lock the resource goods. add () print ("product quantity:", goods. count, "producer thread") cond. yyall () # Wake up all the waiting threads -- in fact, it is to wake up the consumer process cond. release () # unlock the resource time. sleep (self. sleeptime) class Consumer (threading. thread): # consumer class def _ init _ (self, condition, goods, sleeptime = 2): # sleeptime = 2 threading. thread. _ init _ (self) self. cond = condition self. goods = goods self. sleeptime = sleeptime def run (self): cond = self. cond goods = self. goods while True: time. sleep (self. sleeptime) cond. acquire () # Lock the resource while goods. empty (): # if there is no product, let the thread wait for cond. wait () goods. sub () print ("product quantity:", goods. count, "Consumer thread") cond. release () # unlock resource g = Goods () c = threading. condition () pro = Producer (c, g) pro. start () con = Consumer (c, g) con. start ()

Synchronization queue

 

Put method and task_done method, queue has a number of unfinished tasks num, put num + 1 in turn, tasks in turn num-1. When the task is completed, the task ends.

 

Import threadingimport queueimport timeimport random ''' 1. Create a Queue. Queue () instance and fill it with data. 2. Pass the filled data instance to the Thread class, which is created by inheriting threading. Thread. 3. Each time a project is retrieved from the queue and the data and run methods in the thread are used to execute the corresponding work. 4. After this task is completed, use the queue. task_done () function to send a signal to the completed queue of the task. 5. Performing the join operation on the queue actually means that the main program is exited when the queue is empty. '''Class jdThread (threading. thread): def _ init _ (self, index, queue): threading. thread. _ init _ (self) self. index = index self. queue = queue def run (self): while True: time. sleep (1) item = self. queue. get () if item is None: break print ("No.:", self. index, "task", item, "finished") self. queue. task_done () # The task_done method causes the number of unfinished tasks-1q = queue. the Queue (0) ''' initialization function accepts a number as the capacity of the Queue. If a number smaller than or equal to 0 is passed, by default, the capacity of the queue is unlimited. ''' for I in range (2): jdThread (I, q ). start () # two threads simultaneously complete the task for I in range (10): q. put (I) # The put method makes the number of unfinished tasks + 1


 

 


 

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.