python开发之基于thread线程搜索本地文件的方法


Posted in Python onNovember 11, 2015

本文实例讲述了python开发之基于thread线程搜索本地文件的方法。分享给大家供大家参考,具体如下:

先来看看运行效果图:

python开发之基于thread线程搜索本地文件的方法

利用多个线程处理搜索的问题,我们可以发现他很快....

下面是代码部分:

# A parallelized "find(1)" using the thread module.
# This demonstrates the use of a work queue and worker threads.
# It really does do more stats/sec when using multiple threads,
# although the improvement is only about 20-30 percent.
# (That was 8 years ago. In 2002, on Linux, I can't measure
# a speedup. :-( )
# I'm too lazy to write a command line parser for the full find(1)
# command line syntax, so the predicate it searches for is wired-in,
# see function selector() below. (It currently searches for files with
# world write permission.)
# Usage: parfind.py [-w nworkers] [directory] ...
# Default nworkers is 4
import sys
import getopt
import time
import os
from stat import *
import _thread as thread
# Work queue class. Usage:
#  wq = WorkQ()
#  wq.addwork(func, (arg1, arg2, ...)) # one or more calls
#  wq.run(nworkers)
# The work is done when wq.run() completes.
# The function calls executed by the workers may add more work.
# Don't use keyboard interrupts!
class WorkQ:
  # Invariants:
  # - busy and work are only modified when mutex is locked
  # - len(work) is the number of jobs ready to be taken
  # - busy is the number of jobs being done
  # - todo is locked iff there is no work and somebody is busy
  def __init__(self):
    self.mutex = thread.allocate()
    self.todo = thread.allocate()
    self.todo.acquire()
    self.work = []
    self.busy = 0
  def addwork(self, func, args):
    job = (func, args)
    self.mutex.acquire()
    self.work.append(job)
    self.mutex.release()
    if len(self.work) == 1:
      self.todo.release()
  def _getwork(self):
    self.todo.acquire()
    self.mutex.acquire()
    if self.busy == 0 and len(self.work) == 0:
      self.mutex.release()
      self.todo.release()
      return None
    job = self.work[0]
    del self.work[0]
    self.busy = self.busy + 1
    self.mutex.release()
    if len(self.work) > 0:
      self.todo.release()
    return job
  def _donework(self):
    self.mutex.acquire()
    self.busy = self.busy - 1
    if self.busy == 0 and len(self.work) == 0:
      self.todo.release()
    self.mutex.release()
  def _worker(self):
    time.sleep(0.00001)   # Let other threads run
    while 1:
      job = self._getwork()
      if not job:
        break
      func, args = job
      func(*args)
      self._donework()
  def run(self, nworkers):
    if not self.work:
      return # Nothing to do
    for i in range(nworkers-1):
      thread.start_new(self._worker, ())
    self._worker()
    self.todo.acquire()
# Main program
def main():
  nworkers = 4
  #print(getopt.getopt(sys.argv[1:], '-w:'))
  opts, args = getopt.getopt(sys.argv[1:], '-w:')
  for opt, arg in opts:
    if opt == '-w':
      nworkers = int(arg)
  if not args:
    #print(os.curdir)
    args = [os.curdir]
  wq = WorkQ()
  for dir in args:
    wq.addwork(find, (dir, selector, wq))
  t1 = time.time()
  wq.run(nworkers)
  t2 = time.time()
  sys.stderr.write('Total time %r sec.\n' % (t2-t1))
# The predicate -- defines what files we look for.
# Feel free to change this to suit your purpose
def selector(dir, name, fullname, stat):
  # Look for world writable files that are not symlinks
  return (stat[ST_MODE] & 0o002) != 0 and not S_ISLNK(stat[ST_MODE])
# The find procedure -- calls wq.addwork() for subdirectories
def find(dir, pred, wq):
  try:
    names = os.listdir(dir)
  except os.error as msg:
    print(repr(dir), ':', msg)
    return
  for name in names:
    if name not in (os.curdir, os.pardir):
      fullname = os.path.join(dir, name)
      try:
        stat = os.lstat(fullname)
      except os.error as msg:
        print(repr(fullname), ':', msg)
        continue
      if pred(dir, name, fullname, stat):
        print(fullname)
      if S_ISDIR(stat[ST_MODE]):
        if not os.path.ismount(fullname):
          wq.addwork(find, (fullname, pred, wq))
# Call the main program
main()

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

Python 相关文章推荐
简单使用Python自动生成文章
Dec 25 Python
Python的自动化部署模块Fabric的安装及使用指南
Jan 19 Python
理论讲解python多进程并发编程
Feb 09 Python
Python pycharm 同时加载多个项目的方法
Jan 17 Python
Django学习笔记之为Model添加Action
Apr 30 Python
Python中变量的输入输出实例代码详解
Jul 28 Python
python修改字典键(key)的方法
Aug 05 Python
Python Django Cookie 简单用法解析
Aug 13 Python
python 实现二维字典的键值合并等函数
Dec 06 Python
Python自动化测试笔试面试题精选
Mar 12 Python
python3中sys.argv的实例用法
Apr 24 Python
Python中的min及返回最小值索引的操作
May 10 Python
python开发之tkinter实现图形随鼠标移动的方法
Nov 11 #Python
Python复制文件操作实例详解
Nov 10 #Python
Python中对元组和列表按条件进行排序的方法示例
Nov 10 #Python
Python 文件管理实例详解
Nov 10 #Python
Python list操作用法总结
Nov 10 #Python
python控制台中实现进度条功能
Nov 10 #Python
使用Python发送各种形式的邮件的方法汇总
Nov 09 #Python
You might like
受疫情影响 动画《Re从零开始的异世界生活》第二季延期至7月
2020/03/10 日漫
PHP session常见问题集锦及解决办法总结
2007/03/18 PHP
基于PHP服务端图片生成缩略图的方法详解
2013/06/20 PHP
laravel实现一个上传图片的接口,并建立软链接,访问图片的方法
2019/10/12 PHP
tp5.1 框架数据库高级查询技巧实例总结
2020/05/25 PHP
JavaScript的单例模式 (singleton in Javascript)
2010/06/11 Javascript
js导出table到excel同时兼容FF和IE示例
2013/09/03 Javascript
使用jQuery实现的掷色子游戏动画效果
2014/03/14 Javascript
ExtJS4 表格的嵌套 rowExpander应用
2014/05/02 Javascript
javascript中attribute和property的区别详解
2014/06/05 Javascript
js读取cookie方法总结
2014/10/31 Javascript
原生Js实现简易烟花爆炸效果的方法
2015/03/20 Javascript
开启BootStrap学习之旅
2016/05/04 Javascript
使用JS 插件qrcode.js生成二维码功能
2017/02/20 Javascript
angularjs的select使用及默认选中设置
2017/04/08 Javascript
基于vue通用表单解决方案的思考与分析
2019/03/16 Javascript
vue 实现走马灯效果
2019/10/28 Javascript
vue $set 给数据赋值的实例
2019/11/09 Javascript
[43:24]VG vs Serenity 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
[05:08]DOTA2-DPC中国联赛3月6日Recap集锦
2021/03/11 DOTA
详解Python爬虫的基本写法
2016/01/08 Python
Python搭建Spark分布式集群环境
2019/07/05 Python
python线程定时器Timer实现原理解析
2019/11/30 Python
Python Numpy 控制台完全输出ndarray的实现
2020/02/19 Python
Python virtualenv虚拟环境实现过程解析
2020/04/18 Python
利用python对excel中一列的时间数据更改格式操作
2020/07/14 Python
Python通过类的组合模拟街道红绿灯
2020/09/16 Python
python使用scapy模块实现ping扫描的过程详解
2021/01/21 Python
HTML5之SVG 2D入门1—SVG(可缩放矢量图形)概述
2013/01/30 HTML / CSS
健康监测猫砂:Pretty Litter
2017/05/25 全球购物
重新定义牛仔布,100美元以下:Warp + Weft
2018/07/25 全球购物
中国旅游网站:途牛旅游网
2019/09/29 全球购物
在职人员函授期间自我评价分享
2013/11/08 职场文书
商务英语专业求职信
2014/06/26 职场文书
泰山导游词
2015/02/02 职场文书
社区节水倡议书
2015/04/29 职场文书