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使用socket向客户端发送数据的方法
Apr 29 Python
Python闭包的两个注意事项(推荐)
Mar 20 Python
详解Python如何生成词云的方法
Jun 01 Python
Python异步操作MySQL示例【使用aiomysql】
May 16 Python
Python微信操控itchat的方法
May 31 Python
python 实现将多条曲线画在一幅图上的方法
Jul 07 Python
Python2与Python3的区别点整理
Dec 12 Python
PyQt5 closeEvent关闭事件退出提示框原理解析
Jan 08 Python
python如何使用腾讯云发送短信
Sep 17 Python
python利用xlsxwriter模块 操作 Excel
Oct 14 Python
python爬虫线程池案例详解(梨视频短视频爬取)
Feb 20 Python
python实现银行账户系统
Feb 22 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
探讨:如何使用PHP实现计算两个日期间隔的年、月、周、日数
2013/06/13 PHP
thinkphp的URL路由规则与配置实例
2014/11/26 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
2016/11/18 PHP
JS 的应用开发初探(mootools)
2009/12/19 Javascript
十分钟打造AutoComplete自动完成效果代码
2009/12/26 Javascript
JS 删除字符串最后一个字符的实现代码
2014/02/20 Javascript
使用jQuery获得内容以及内容的属性
2015/02/26 Javascript
浅谈JavaScript 的执行顺序
2015/08/07 Javascript
功能强大的jquery.validate表单验证插件
2016/11/07 Javascript
利用JQuery阻止事件冒泡
2016/12/01 Javascript
jQuery得到多个值只能用取Class ,不能用取ID的方法
2016/12/04 Javascript
JQuery异步提交表单与文件上传功能示例
2017/01/12 Javascript
jquery实现input框获取焦点的简单实例
2017/01/26 Javascript
基于node.js依赖express解析post请求四种数据格式
2017/02/13 Javascript
浅谈JS如何实现真正的对象常量
2017/06/25 Javascript
Bootstrap Table从零开始
2017/06/30 Javascript
详谈for循环里面的break和continue语句
2017/07/20 Javascript
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2018/05/15 Javascript
vue中动态添加class类名的方法
2018/09/05 Javascript
使用weixin-java-miniapp配置进行单个小程序的配置详解
2019/03/29 Javascript
[06:11]2014DOTA2国际邀请赛 专访团结一心的VG战队
2014/07/21 DOTA
[38:27]完美世界DOTA2联赛PWL S2 Forest vs FTD.C 第二场 11.26
2020/11/30 DOTA
python实现爬取千万淘宝商品的方法
2015/06/30 Python
浅谈五大Python Web框架
2017/03/20 Python
python的pdb调试命令的命令整理及实例
2017/07/12 Python
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
python实现机器学习之元线性回归
2018/09/06 Python
利用Python正则表达式过滤敏感词的方法
2019/01/21 Python
Pyspark获取并处理RDD数据代码实例
2020/03/27 Python
python subprocess pipe 实时输出日志的操作
2020/12/05 Python
深入解析HTML5中的Blob对象的使用
2015/09/08 HTML / CSS
html+js 实现markdown编辑器效果
2019/10/23 HTML / CSS
美国生日蛋糕店:Bake Me A Wish!
2017/02/08 全球购物
荷兰睡眠专家:Beter Bed
2020/11/23 全球购物
学生会离职感言
2014/02/11 职场文书
使用numpy实现矩阵的翻转(flip)与旋转
2021/06/03 Python