在之前的blog中,曾经写到过关于搜索本地文件的技术文章

如:

java开发_快速搜索本地文件_小应用程序

python开发_搜索本地文件信息写入文件

下面说说python中关于线程来搜索本地文件

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

========================================================

下面是代码部分:

========================================================

 # 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()

更多信息http://www.oschina.net/code/explore/Python-3.1.3/Demo/threads/find.py

========================================================

More reading,and english is important.

I'm Hongten

大哥哥大姐姐,觉得有用打赏点哦!多多少少没关系,一分也是对我的支持和鼓励。谢谢。
Hongten博客排名在100名以内。粉丝过千。
Hongten出品,必是精品。

E | hongtenzone@foxmail.com  B | http://www.cnblogs.com/hongten

========================================================

python开发_thread_线程_搜索本地文件的更多相关文章

  1. python开发_搜索本地文件信息写入文件

    功能:#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件 #然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的 #文件(如果文件不存在,则建立相应的文件)中 之前 ...

  2. python开发_thread_线程基础

    说到线程,我们要知道啥是串行,啥是并行程序 举个例子: 串行程序,就是一个一个的执行程序 #python threading import time ''' 每一秒中,输出:this is a dem ...

  3. win10无法搜索本地文件,修复方法?

    win10无法搜索本地文件,实在太不方便了,网上查了一圈没几个方法有效的,筛选出来2个成功解决的问题,具体是哪个起到作用,不太清楚,都放上来,大家自行选择! 方法1:按“Windows+ X”后选择“ ...

  4. 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件

    演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...

  5. 重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件

    原文:重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件 [源码下载] 重新想象 Windows 8 Store Apps ( ...

  6. 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容

    在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...

  7. Python开发环境Wing IDE搜索工具介绍

    Wing IDE编辑器的搜索工具提供了一个基于友好GUI的搜索和替换工具. 某些情况下搜索可能会跨越整个文件,也有可能被限制到当前所选择的区域:可以区分大小写,也可以设置为不区分:可以被限制为只匹配整 ...

  8. Android开发 MediaPlayer入门_播放本地视频

    前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...

  9. Python开发基础-Day5-字符编码、文件处理和函数基础(草稿)

    字符编码 为什么要有字符编码? 字符编码是为了让计算机能识别我们人写的字符,因为计算机只认识高低电平,也就是二进制数"0","1". 一个文件用什么编码方式存储 ...

随机推荐

  1. CentOS安装ANT

    第1步:下载ant apache-ant-1.9.2-bin.tar.gz http://archive.apache.org/dist/ant/binaries/ 第2步:解压 tar -zxvf ...

  2. 【转】WCF光芒下的Web Service

    WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客 ...

  3. 高性能.NET MVC之QMVC!

    ASP.NET!这个词代表者一个单词Fat!因为他总是捆绑着太多的太多的类,太多太多的各种功能!你也许会用到,如果你反编译或阅读他们开源的源码,你会不会犹如在大海中找不到方向?不管是Web form ...

  4. 数据库-mysql事务

    MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...

  5. CSS--布局模型,颜色值,长度值

    目录 布局模型 流动模型(Flow) 浮动模型 (Float) 层模型(Layer) 颜色值 长度值  一.布局模型 网页布局模型:个人理解,就是对html中各个元素进行一个排列.而排列的方法可以分为 ...

  6. IOS使用命令行打包

    1.安装证书 develop:调试证书,包含调试信息 安装时需要证书已加入设备UDID或是越狱设备才能安装. distribution:正式证书,发布到appstore所用,屏蔽了调试信息,要想测试安 ...

  7. Jdk 和 Tomcat的 安装。

    1.再分发服务器上下载JDK,然后利用xftp上传到聚石塔等 2. 解压: tar -zxvf jdk-8u121-linux-x64.tar.gz 3.配置环境变量: export JAVA_HOM ...

  8. python软件依赖关系

    caffe:numpy,scikit-image opencv:numpy

  9. new[] 到底做了什么?

    #include<iostream> #include<cstdlib> using std::cout; using std::endl; using std::hex; c ...

  10. javascript大神修炼记(6)——OOP思想(继承)

    读者朋友们大家好,我们今天这一讲就接着前面的封装继续讲解,今天就是在前面内容上面的升级,OOP思想中的继承,我们就先来解释一下继承到底是什么意思,我们在什么地方会用到继续. 继承就是,后代继续祖先的一 ...