Home > Backend Development > Python Tutorial > Code examples of producer and consumer operations of python Condition objects

Code examples of producer and consumer operations of python Condition objects

Y2J
Release: 2017-05-04 11:19:09
Original
1813 people have browsed it

This article mainly introduces the producer and consumer operations of python condition variables, and analyzes the concepts, principles, and thread operation-related techniques of Python condition variables based on specific examples. Friends in need can refer to the following

The example in this article describes the producer and consumer operations of python condition variables. Share it with everyone for your reference, the details are as follows:

Mutex lock is the simplest thread synchronization mechanism. In the face of complex thread synchronization problems, Python also provides Condition objects. Condition is called a condition variable. In addition to providing acquire and release methods similar to Lock, it also provides wait and notify methods. The thread first acquires a condition variable and then determines some conditions. If the conditions are not met, wait; if the conditions are met, perform some processing to change the conditions, and notify other threads through the notify method. Other threads in the wait state will re-judge the conditions after receiving the notification. This process is repeated continuously to solve complex synchronization problems.

It can be considered that the Condition object maintains a lock (Lock/RLock) and a waiting pool. The thread obtains the Condition object through acquire. When the wait method is called, the thread will release the lock inside the Condition and enter the blocked state (but it will not actually block the current thread) and record this thread in the waiting pool. When the notify method is called, the Condition object will select a thread from the waiting pool and notify it to call the acquire method to try to acquire the lock.

The constructor of the Condition object can accept a Lock/RLock object as a parameter. If not specified, the Condition object will create an RLock internally.

The classic problem of thread synchronization - the problem of producers and consumers can be easily solved using condition variables.

import threading
import time
class Producer(threading.Thread):
  def init(self):
    threading.Thread.init(self)
  def run(self):
    global count
    while True:
      con.acquire()
      if count <20:
        count += 1
        print self.name," Producer product 1,current is %d" %(count)
        con.notify()
      else:
        print self.name,"Producer say box is full"
        con.wait()
      con.release()
      time.sleep(1)
class Consumer(threading.Thread):
  def init(self):
    threading.Thread.init(self)
  def run(self):
    global count
    while True:
      con.acquire()
      if count>4:
        count -=4
        print self.name,"Consumer consume 4,current is %d" %(count)
        con.notify()
      else:
        con.wait()
        print self.name," Consumer say box is empty"
      con.release()
      time.sleep(1)
count = 0
con = threading.Condition()
def test():
  for i in range(1):
    a = Consumer()
    a.start()
  for i in range(1):
    b =Producer()
    b.start()
if name==&#39;main&#39;:
  test()
Copy after login

The above code assumes that consumers consume quickly, and the output result is:

The above is the detailed content of Code examples of producer and consumer operations of python Condition objects. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template