本文实例讲述了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开发之thread线程基础实例入门,包括了python开发之thread线程基础实例入门的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了python开发之thread线程基础。分享给大家供大家参考,具体如下: 说到线程,我们要知道啥是串行,啥是并行程序 举个例子: 串行程序,就是一个一个的执行程序 运行结果如下: 并行程序,就是很多个程序在同一时间(宏观)一起执行
本文向大家介绍iOS开发教程之XLForm的基本使用方法,包括了iOS开发教程之XLForm的基本使用方法的使用技巧和注意事项,需要的朋友参考一下 前言 在iOS开发中,开发"表单"界面,字段稍微多一点的一般都用UITableView来做,而XLForm就是这样一个框架,它是创建动态表格视图最牛逼的iOS库, 用它实现表单功能,非常简单,省心省力。但是很可惜,搜索了很多文章都只是翻译官方文档,很多
本文向大家介绍Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法,包括了Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法的使用技巧和注意事项,需要的朋友参考一下 废话不多说,直接上代码吧! 以上这篇Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教
我正在编写一种方法来搜索列表形式的文字文件,搜索用户输入的文字,但如果找到一个字母,程序将返回一个肯定的结果。例如,如果我搜索“f”,当没有单词时,它将返回字典中有一个单词“f”
功能介绍 本地搜索(LocalSearch)是按照用户的兴趣选择相应类别,查询相关的兴趣点(POI)信息的服务。本地搜索服务可以实现根据用户输入的POI关键词在指定范围“city”内查找与之匹配的地理兴趣点,并将其结果有序返回的功能。 适用场景 已知POI的关键字和POI所在城市进行搜索。 例如:已知“KeyWords=超图软件”,“city=北京”,可搜索附近一定范围内的地点。 使用本地搜索服务
本地搜索服务可以根据用户输入的POI关键词在指定范围“city”内查找与之匹配的地理兴趣点,并将其结果有序返回。 使用服务前需要先申请 key。 资源服务地址:https://www.supermapol.com/iserver/services/localsearch/rest/searchdatas/China/poiinfos 资源接口参数说明 本地搜索服务支持GET和HEAD请求,支持js