Recently in to see Python Basics , Just saw the fast content of producers and consumers , Video used queue
Module to handle , Here's what I learned
Producer and consumer is a relatively easy to understand concept , For example, one end of the swimming pool comes in and the other end comes out , It's a typical example .
The code in the video is mainly the following :
# ecoding=utf-8
# Author: Weng Yanbin | Sven_Weng
# Email : [email protected]
# Web : http://wybblog.applinzi.com
from threading import current_thread, Thread
import time
import random
import queue
q = queue.Queue(5)
class Productor(Thread):
def run(self):
name = current_thread().getName()
nums = range(100)
while 1:
nowput = random.choice(nums)
if q.full(): # Stop production when the message queue is full
print " The queue has reached its maximum {0}".format(q.qsize())
time.sleep(10)
q.put(nowput)
print " producer {0} Produced {1}".format(name, nowput)
sl = random.choice([1, 2, 3])
time.sleep(sl)
print " The producer has a rest {0} second ".format(sl)
class Consumer(Thread):
def run(self):
name = current_thread().getName()
while 1:
if q.empty(): # When the message queue is empty, consumption is suspended
print " The queue is empty , Stop spending "
time.sleep(5)
num = q.get()
q.task_done()
print " consumer {0} The consumption {1}".format(name, num)
sl = random.choice([1, 2, 3])
time.sleep(sl)
print " Consumers have a rest {0} second ".format(sl)
if __name__ == '__main__':
p1 = Productor()
p1.start()
p2 = Productor()
p2.start()
c1 = Consumer()
c1.start()
c2 = Consumer()
c2.start()
c3 = Consumer()
c3.start()
It's using threading
Module to produce content while consuming content , The whole code is relatively simple , But it's not particularly easy to understand , Especially when novices understand, they are not intuitive enough .
Message queue can be understood as a complex list
, For example, to achieve first in first out , So every time you return list[0]
That's it , Empathy , If you want to achieve last in, first out , So every time you return list[len(list)]
That's it , It's easier to understand this way .
Of course , Compared with message queuing list
It's a little complicated , But the principle is basically like this , such as queue
It uses threading
Module to achieve , More complex message queues , such as Python
More of them are used in Chinese celery
, Optional message queues are RabbitMQ
perhaps Redis
Given the complexity of the examples in the video , I wrote a simple and intuitive example myself , use Flask
I wrote a simple one Web
Standardized Message Queuing service .
from flask import Flask, jsonify, request
from queue import Queue
app = Flask(__name__)
@app.before_first_request
def init_queue():
app.q = Queue(5)
@app.route('/')
def hello_world():
data = {
"code": "0000",
"queue_count": app.q.qsize()
}
return jsonify(data)
@app.route('/put')
def put_to_queue():
num = request.args['num']
print num
if app.q.full():
data = {
"code": "0001",
"msg": "The Queue is full"
}
else:
app.q.put(num)
data = {
"code": "0000",
"msg": "Success put {1} to the Queue, current count is {0}".format(app.q.qsize(), num)
}
return jsonify(data)
@app.route('/get')
def get_from_queue():
if app.q.empty():
data = {
"code": "0002",
"msg": "The Queue is empty"
}
else:
data = {
"code": "0000",
"msg": "Success get from the Queue, current count is {0}".format(app.q.qsize()),
"num": app.q.get()
}
app.q.task_done()
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)
First, call... At startup before_first_request
To initialize the queue , Put it in app.q
In this global variable , Perform different queue operations with different requests , You can see different results on the page . Number of squadrons in the demonstration example 5 One is full , Re execution put
It will return an error message , Empathy , If it is empty , It will also return an error message .