zoukankan      html  css  js  c++  java
  • 【Python】多进程-队列

    #练习:队列
    from multiprocessing import Process, Queue 
    
    def offer(queue): 
      # 入队列
      queue.put("Hello World") 
    
    if __name__ == '__main__': 
      # 创建一个队列实例
      q = Queue()
      p = Process(target = offer, args = (q,)) 
      p.start() 
      print q.get() # 出队列
      p.join()
    
    
    #练习
    import time
    from multiprocessing import Process, Queue 
    
    def set_data(queue): 
      # 入队列
      for i in range(10):
          time.sleep(2)
          queue.put("Hello World"+str(i)) 
    
    def get_data(queue): 
      for i in range(10):
      # 入队列
          time.sleep(1)
          print queue.get("Hello World") 
    
    
    if __name__ == '__main__': 
      # 创建一个队列实例
      q = Queue()
      p1 = Process(target = set_data, args = (q,)) 
      p2 = Process(target = get_data, args = (q,))
      p1.start() 
      p2.start()
     
      p1.join()
      p2.join()
      print u"队列是否为空?",q.empty()
    
    
    #练习:写两个队列,一个用来写,一个用来读,一边写,一边读
    from multiprocessing import Process, Queue
    
    def input_queue(queue_input):
        for i in range(11):
            if queue_input.full():
                print "队列已经满啦"
            else:
                queue_input.put(i)
                #print queue_input
    
    def get_queue(queue_get):
        for i in range(11):
            if queue_get.empty():
                print u"队列已经空啦"
            else:
                print queue_get.get()
    
    
    if __name__=="__main__":
        q=Queue()
        p1=Process(target=input_queue,args=(q,)) #写进程和读进程是同时执行的,只不过通过sleep时间来控制读和写的速度
        p2=Process(target=get_queue,args=(q,))
    
        p1.start()
        p2.start()
    
        p2.join()
        p2.join()
  • 相关阅读:
    greenplum 常用整理
    postgresql 常用整理
    数据源整理
    hive 以like方式建表(携带表结构)
    kudu 常用整理
    mysql常用整理
    Linux查看文件内容的常用方式(more,less,head,tail,cat)
    python2.7 安装docx错误
    python2.7 安装搜索引擎错误
    mysql数据库安装错误
  • 原文地址:https://www.cnblogs.com/jingsheng99/p/8713304.html
Copyright © 2011-2022 走看看