美文网首页
python 使用 threading 多线程模拟 MQ

python 使用 threading 多线程模拟 MQ

作者: 猿来是八阿哥 | 来源:发表于2019-07-29 13:33 被阅读0次
    # -*- coding: utf-8 -*-
    import time
    import random
    import threading
    class PythonMQ(object):
        queue = list()
        a_queue_producer_thread = False
        a_queue_consumer_thread = False
        def __init__(self):
            self.a_queue_producer_thread = threading.Thread(target=self.a_queue_producer)
            self.a_queue_consumer_thread = threading.Thread(target=self.a_queue_consumer)
    
        def a_queue_producer(self):
            while True:
                self.queue.append(random.randint(1, 1000))
                time.sleep(1)
    
        def a_queue_consumer(self):
            while True:
                try:
                    value = self.queue.pop()
                    print 'pop: ' + str(value) + ', len=' + str(len(self.queue))
                except IndexError as ie:
                    time.sleep(0.1)
    
        def start(self):
            self.a_queue_producer_thread.start()
            self.a_queue_consumer_thread.start()
    
    if __name__ == "__main__":
        mq = PythonMQ()
        mq.start()
    
    

    相关文章

      网友评论

          本文标题:python 使用 threading 多线程模拟 MQ

          本文链接:https://www.haomeiwen.com/subject/guhbrctx.html