zoukankan      html  css  js  c++  java
  • python 队列

    队列--存放对象的容器

    #coding:utf-8
    
    import queue
    import time
    
    q = queue.Queue()
    
    for i in range(10):
        q.put(i)
    
    while not q.empty():
        print('从队列中取出的元素: %s' % q.get())
        time.sleep(0.5)

    生产者消费者模型

    #coding:utf-8
    import time
    import threading
    import queue
    import os
    
    from threading import Thread
    
    q = queue.Queue()
    
    
    class Consumer(Thread):
        
        def __init__(self, q):
            Thread.__init__(self)
            self.q = q
            
        def run(self):
            while True:
                msg = self.q.get()
                print(msg)
                if isinstance(msg, str) and msg == 'finish':
                    break
                os.rename(msg , msg+'.bak')
            
            print('byebye')
    
    
    class Producer(Thread):
        
        def __init__(self, q):
            Thread.__init__(self)
            self.q = q
            
        def run(self):
    
            start_time = time.time()
            i = 0
            while time.time() - start_time < 10:
                fullpath = r"C:UsersMartinDesktopxxx	est\%s" % str(time.time())
                i += 1
                f = open(fullpath, 'w')
                f.close()
                self.q.put(fullpath)
                time.sleep(1)
            
            self.q.put('finish')
    
    
    worker = Consumer(q)
    worker.start()
    
    producer = Producer(q)
    producer.start()

  • 相关阅读:
    服务器负载均衡的基本功能和实现原理
    二分查找
    TCP的运输连接管理
    linux常用命令
    XX公司在线笔试题编程题之一
    java对象转json格式
    Java多线程并发技术
    进程同步与通信
    单例模式的C++实现
    rsyncd启动脚本
  • 原文地址:https://www.cnblogs.com/hellojackyleon/p/9691306.html
Copyright © 2011-2022 走看看