作家
登录

Python一行代码完成并行任务

作者: 来源: 2017-04-14 10:02:25 阅读 我要评论

  •   
  •  
  • def Producer(): 
  •  
  •     # Queue is used to share items between 
  •  
  •     # the threads. 
  •  
  •     queue = Queue.Queue() 
  •  
  •   
  •  
  •     # Create an instance of the worker 
  •  
  •     worker = Consumer(queue) 
  •  
  •     # start calls the internal run() method to 
  •  
  •     # kick off the thread 
  •  
  •     worker.start() 
  •  
  •   
  •  
  •     # variable to keep track of when we started 
  •  
  •     start_time = time.time() 
  •  
  •     # While under 5 seconds.. 
  •  
  •     while time.time() - start_time < 5: 
  •  
  •         # "Produce" a piece of work and stick it in 
  •  
  •         # the queue for the Consumer to process 
  •  
  •         queue.put('something at %s' % time.time()) 
  •  
  •     # Sleep a bit just to avoid an absurd number of messages 
  •  
  •     time.sleep(1) 
  •  
  •   
  •  
  •     # This the "poison pill" method of killing a thread. 
  •  
  •     queue.put('quit'
  •  
  •     # wait for the thread to close down 
  •  
  •     worker.

      推荐阅读

      理解深度学习的钥匙 –启蒙篇

    神经收集是有史以来创造的最优美的编程范式之&#12032;。在传统的编程&#12101;法中,我们告诉计算机做什么,把&#12068;问题分成很多小的、准肯定义的义务,计算机可以很轻易地履行。比拟之>>>详细阅读


    本文标题:Python一行代码完成并行任务

    地址:http://www.17bianji.com/lsqh/34782.html

  • 关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)