• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python基于queue和threading实现多线程下载实例

python基于queue和threading实现多线程下载实例

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python threading,python threading模块,python3 threading,python中threading,threading等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python基于queue和threading实现多线程下载的方法,分享给大家供大家参考。具体方法如下:

主代码如下:

  #download worker 
  queue_download = Queue.Queue(0) 
  DOWNLOAD_WORKERS = 20 
  for i in range(DOWNLOAD_WORKERS): 
    DownloadWorker(queue_download).start() #start a download worker 
     
  for md5 in MD5S: 
    queue_download.put(md5) 
  for i in range(DOWNLOAD_WORKERS): 
    queue_download.put(None) 

</div>

其中downloadworkers.py
类继承 threading.Thread,重载run方法..在__init__中调用threading.Thread.__init__(self),
在run方法中实现耗时的操作

import threading 
import Queue 
import md5query 
import DOM 
import os,sys 

class DownloadWorker(threading.Thread): 
  """""" 
 

  def __init__(self, queue): 
    """Constructor""" 
    self.__queue = queue 
    threading.Thread.__init__(self) 
 
 
  def run(self): 
    while 1: 
      md5 = self.__queue.get() 
      if md5 is None: 
        break #reached end of queue 
      #this is a time-cost produce 
      self._down(md5) 
 
      print "task:", md5, "finished" 
 
  def _down(self, md5): 
    config = { 
      'input':sys.stdin,  
      'output':'./samples',  
      'location':'xxx',  
      'has-fn':False,  
      'options':{'connect.timeout':60, 'timeout':3600},  
      'log':file('logs.txt', 'w'),  
    } 
    print 'download %s...' % (md5) 
    try: 
      data = downloadproc(config['location'], config['options'])#我的下载过程 
      if data: 
        dom, fileData = md5query.splited(data) 
        filename = md5 
        if config['has-fn']: 
          filename = '%s_%s' % (md5, dom.nodeValue2('xxxxxxx', '').encode('utf-8'))#这是我的下载的方法 
        f = file(os.path.join(config['output'], filename), 'w') 
        f.write(fileData) 
        f.close() 
 
        print '%s\tok' % (md5) 
      else: 
        print>>config['log'], '%s\t%s' % (md5, 'failed') 
    except Exception, e: 
      print>>config['log'], '%s\t%s' % (md5, str(e))

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 在Python中通过threading模块定义和调用线程的方法
  • Python中线程编程之threading模块的使用详解
  • 举例详解Python中threading模块的几个常用方法
  • Python中threading模块join函数用法实例分析
  • python中threading超线程用法实例分析
  • python threading模块操作多线程介绍
  • python threading模块操作多线程介绍
  • Python多线程编程(三):threading.Thread类的重要函数和方法
  • Python多线程编程(一):threading模块综述
  • Python THREADING模块中的JOIN()方法深入理解

相关文章

  • Python实现字符串格式化的方法小结
  • windows 10下安装搭建django1.10.3和Apache2.4的方法
  • 利用PyInstaller将python程序.py转为.exe的方法详解
  • python笔记(2)
  • 玩转python爬虫之爬取糗事百科段子
  • 零基础写python爬虫之爬虫编写全记录
  • python简单程序读取串口信息的方法
  • Python编程之event对象的用法实例分析
  • 详解python 发送邮件实例代码
  • python抓取网页中的图片示例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python里隐藏的“禅”
    • python列表操作使用示例分享
    • Python多线程、异步+多进程爬虫实现代码
    • Python常用列表数据结构小结
    • 使用PyV8在Python爬虫中执行js代码
    • python控制台显示时钟的示例
    • 用map函数来完成Python并行任务的简单示例
    • Python的条件语句与运算符优先级详解
    • python算法学习之桶排序算法实例(分块排序)
    • python静态方法实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有