• 队列


    #coding=utf8
    
    import Queue
    
    q = Queue.LifoQueue()
    for i in range(5):
        q.put(i)
    
    while not q.empty():
        print q.get()
    
    print 
    

      

    #coding=utf8
    
    import Queue, threading
    
    q = Queue.PriorityQueue()
    
    class Job(object):
        def __init__(self, priority, description):
            self.priority = priority
            self.description = description
            print 'new job',description
        
        def __cmp__(self, other):
            return cmp(self.priority, other.priority)
    
    q.put( Job(3, 'mid') )
    q.put( Job(10, 'low') )
    q.put( Job(1, 'import'))
    
    def process_job(q):
        while True:
            n_j = q.get()
            print 'Processing job:%s \n' % n_j.description
            q.task_done()
    
    works = [
        threading.Thread(target=process_job, args=(q,)),
        threading.Thread(target=process_job, args=(q,)),
        threading.Thread(target=process_job, args=(q,)),
    ]
    
    for w in works:
        w.setDaemon(True)
        w.start()
    
    q.join()
    

      

    • join:如在一个线程B中调用threada.join(),则threada结束后,线程B才会接着threada.join()往后运行。
    • setDaemon:主线程A启动了子线程B,调用b.setDaemaon(True),则主线程结束时,会把子线程B也杀死,与C/C++中得默认效果是一样的。
  • 相关阅读:
    数据表格优化
    vue数组和对象的监听变化
    python flask框架搭建以及大佬代码参考
    简单爬虫
    srs的基本配置
    记录飞天程序库调用
    面试题 递归算法1+2+....+100求和
    下载列表组件
    Prometheus之系统安装,启动
    nginx之日志
  • 原文地址:https://www.cnblogs.com/bjdxy/p/2865740.html
Copyright © 2020-2023  润新知