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迭代器的使用方法实例
Nov 21 Python
python追加元素到列表的方法
Jul 28 Python
python3 实现的人人影视网站自动签到
Jun 19 Python
python与php实现分割文件代码
Mar 06 Python
Python常见异常分类与处理方法
Jun 04 Python
Python网络编程之TCP套接字简单用法示例
Apr 09 Python
Python实现求解括号匹配问题的方法
Apr 17 Python
IntelliJ IDEA安装运行python插件方法
Dec 10 Python
django 消息框架 message使用详解
Jul 22 Python
解决python 虚拟环境删除包无法加载的问题
Jul 13 Python
详解Python openpyxl库的基本应用
Feb 26 Python
python批量更改目录名/文件名的方法
Apr 18 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
php2html php生成静态页函数
2008/12/08 PHP
PHP连接MySQL进行增、删、改、查操作
2017/02/19 PHP
解决Laravel自定义类引入和命名空间的问题
2019/10/15 PHP
用js判断页面是否加载完成实现代码
2012/12/11 Javascript
javascript中基本类型和引用类型的区别分析
2015/05/12 Javascript
理解Javascript文件动态加载
2016/01/29 Javascript
JavaScript知识点总结(六)之JavaScript判断变量数据类型
2016/05/31 Javascript
js replace(a,b)之替换字符串中所有指定字符的方法
2016/08/17 Javascript
从0开始学Vue
2016/10/27 Javascript
Bootstrap3 datetimepicker控件使用实例
2016/12/13 Javascript
js获取ip和地区
2017/03/10 Javascript
详解React-Native解决键盘遮挡问题(Keyboard遮挡问题)
2017/07/13 Javascript
浅谈nodejs中的类定义和继承的套路
2017/07/26 NodeJs
Bootstrap Table 在指定列中添加下拉框控件并获取所选值
2017/07/31 Javascript
详解jQuery同步Ajax带来的UI线程阻塞问题及解决办法
2017/08/09 jQuery
vue路由嵌套的SPA实现步骤
2017/11/06 Javascript
关于axios不能使用Vue.use()浅析
2018/01/12 Javascript
vue3.0 CLI - 2.2 - 组件 home.vue 的初步改造
2018/09/14 Javascript
[49:27]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第一场
2018/04/05 DOTA
win10系统中安装scrapy-1.1
2016/07/03 Python
python脚本作为Windows服务启动代码详解
2018/02/11 Python
Python计算机视觉里的IOU计算实例
2020/01/17 Python
Python tkinter 下拉日历控件代码
2020/03/04 Python
python使用gdal对shp读取,新建和更新的实例
2020/03/10 Python
如何卸载python插件
2020/07/08 Python
新任教师自我鉴定
2014/02/24 职场文书
学术会议主持词
2014/03/17 职场文书
党日活动总结
2014/05/07 职场文书
市场营销专业求职信
2014/06/17 职场文书
本科毕业论文致谢怎么写
2015/05/14 职场文书
师范生见习总结范文
2015/06/23 职场文书
2015年教师国培感言
2015/08/01 职场文书
《别在吃苦的年纪选择安逸》读后感3篇
2019/11/30 职场文书
《分一些蚊子进来》读后感3篇
2020/01/09 职场文书
解决python绘图使用subplots出现标题重叠的问题
2021/04/30 Python
详解Nginx的超时keeplive_timeout配置步骤
2022/05/25 Servers