Implement the producer and consumer model with 64 lines of code

I can only say that python is too powerful


General idea:

Suppose there are 10 data containers.


Producer:

Obtain an idle container, wait when there is no idle container, and produce a data and fill it into the container when there is an idle container.


consumer:

Obtain non-empty containers, take out non-empty containers to consume, and wait if there are none.



#! /usr/bin/env python
# -*- coding: utf-8 -*-
# author:曹建峰<[email protected]>

import random
import threading
import time

BUF_LEN = 10
datas = []
empty_semaphore = threading._Semaphore(BUF_LEN)
full_semaphore = threading._Semaphore(0)
work_lock = threading.Lock()

def producer():
    while True:
        if empty_semaphore.acquire():

            work_lock.acquire()
            data = int(random.random()*100)
            datas.append(data)
            time.sleep(1)
            print "P:%d ; datalen = %d" % (data,len(datas))
            printDatas()
            work_lock.release()

            full_semaphore.release()

def cunsumer():
    while True:
        if full_semaphore.acquire():

            work_lock.acquire()
            data = datas.pop(0)
            time.sleep(1)
            print "C:%d ; datalen = %d" % (data,len(datas))
            printDatas()
            work_lock.release()

            empty_semaphore.release()

def printDatas():
    for data in datas:
        print "+---+"
        print "|%3d|" % data
    print "+---+"

if __name__ == "__main__":
    p = threading.Thread(target=producer)
    c = threading.Thread(target=cunsumer)
    threads = [p,c]
    for trd in threads:
        trd.setDaemon(True)
        trd.start()

    alive = False
    while 1:
        time.sleep(1)
        for trd in threads:
            if trd.isAlive():
                alive = True
                break
        if not alive:
            break

Output result:


$ python pctest.py 
P:76 ; datalen = 1
+---+
| 76|
+---+
P:80 ; datalen = 2
+---+
| 76|
+---+
| 80|
+---+
C:76 ; datalen = 1
+---+
| 80|
+---+
C:80 ; datalen = 0
+---+
P:37 ; datalen = 1
+---+
| 37|
+---+
P:46 ; datalen = 2
+---+
| 37|
+---+
| 46|
+---+
C:37 ; datalen = 1
+---+
| 46|
+---+
C:46 ; datalen = 0
+---+
P:8 ; datalen = 1
+---+
|  8|
+---+
P:27 ; datalen = 2
+---+
|  8|
+---+
| 27|
+---+
P:61 ; datalen = 3
+---+
|  8|
+---+
| 27|
+---+
| 61|
+---+
C:8 ; datalen = 2
+---+
| 27|
+---+
| 61|
+---+
P:79 ; datalen = 3
+---+
| 27|
+---+
| 61|
+---+
| 79|
+---+
C:27 ; datalen = 2
+---+
| 61|
+---+
| 79|
+---+
P:1 ; datalen = 3
+---+
| 61|
+---+
| 79|
+---+
|  1|
+---+
C:61 ; datalen = 2
+---+
| 79|
+---+
|  1|
+---+
C:79 ; datalen = 1
+---+
|  1|
+---+
P:94 ; datalen = 2
+---+
|  1|
+---+
| 94|
+---+
P:61 ; datalen = 3
+---+
|  1|
+---+
| 94|
+---+
| 61|
+---+
P:66 ; datalen = 4
+---+
|  1|
+---+
| 94|
+---+
| 61|
+---+
| 66|
+---+
C:1 ; datalen = 3
+---+
| 94|
+---+
| 61|
+---+
| 66|
+---+
P:61 ; datalen = 4
+---+
| 94|
+---+
| 61|
+---+
| 66|
+---+
| 61|
+---+
P:37 ; datalen = 5
+---+
| 94|
+---+
| 61|
+---+
| 66|
+---+
| 61|
+---+
| 37|
+---+
P:92 ; datalen = 6
+---+
| 94|
+---+
| 61|
+---+
| 66|
+---+
| 61|
+---+
| 37|
+---+
| 92|
+---+
C:94 ; datalen = 5
+---+
| 61|
+---+
| 66|
+---+
| 61|
+---+
| 37|
+---+
| 92|
+---+
C:61 ; datalen = 4
+---+
| 66|
+---+
| 61|
+---+
| 37|
+---+
| 92|
+---+


Guess you like

Origin blog.csdn.net/windcao/article/details/47911869