python開(kāi)發(fā)之基于thread線(xiàn)程搜索本地文件的方法
本文實(shí)例講述了python開(kāi)發(fā)之基于thread線(xiàn)程搜索本地文件的方法。分享給大家供大家參考,具體如下:
先來(lái)看看運(yùn)行效果圖:
利用多個(gè)線(xiàn)程處理搜索的問(wèn)題,我們可以發(fā)現(xiàn)他很快....
下面是代碼部分:
# 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()
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python實(shí)現(xiàn)單線(xiàn)程多任務(wù)非阻塞TCP服務(wù)端
- Python實(shí)現(xiàn)簡(jiǎn)單的多任務(wù)mysql轉(zhuǎn)xml的方法
- python多任務(wù)及返回值的處理方法
- python多任務(wù)之協(xié)程的使用詳解
- Python協(xié)程操作之gevent(yield阻塞,greenlet),協(xié)程實(shí)現(xiàn)多任務(wù)(有規(guī)律的交替協(xié)作執(zhí)行)用法詳解
- python實(shí)現(xiàn)通過(guò)隊(duì)列完成進(jìn)程間的多任務(wù)功能示例
- Python實(shí)現(xiàn)多線(xiàn)程下載文件的代碼實(shí)例
- 對(duì)Python多線(xiàn)程讀寫(xiě)文件加鎖的實(shí)例詳解
- Python多線(xiàn)程下載文件的方法
- Python多線(xiàn)程同步---文件讀寫(xiě)控制方法
- python 多線(xiàn)程將大文件分開(kāi)下載后在合并的實(shí)例
- python多線(xiàn)程案例之多任務(wù)copy文件完整實(shí)例
相關(guān)文章
用Python編寫(xiě)腳本使IE實(shí)現(xiàn)代理上網(wǎng)的教程
這篇文章主要介紹了用Python編寫(xiě)腳本使IE實(shí)現(xiàn)代理上網(wǎng)的教程,“著名的”goagent代理也是基于同樣原理實(shí)現(xiàn),需要的朋友可以參考下2015-04-04matplotlib畫(huà)混淆矩陣與正確率曲線(xiàn)的實(shí)例代碼
混淆矩陣也稱(chēng)誤差矩陣,是表示精度評(píng)價(jià)的一種標(biāo)準(zhǔn)格式,下面這篇文章主要給大家介紹了關(guān)于matplotlib畫(huà)混淆矩陣與正確率曲線(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-06-06Python面向?qū)ο笾?lèi)和對(duì)象實(shí)例詳解
這篇文章主要介紹了Python面向?qū)ο笾?lèi)和對(duì)象,結(jié)合實(shí)例形式詳細(xì)分析了Python面向?qū)ο笙嚓P(guān)的繼承、多態(tài)、類(lèi)及對(duì)象等概念、原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12用map函數(shù)來(lái)完成Python并行任務(wù)的簡(jiǎn)單示例
這篇文章主要介紹了用map函數(shù)來(lái)完成Python并行任務(wù)的簡(jiǎn)單示例,多線(xiàn)程和多進(jìn)程編程的問(wèn)題一直都是Python中的熱點(diǎn)和難點(diǎn),需要的朋友可以參考下2015-04-04一篇文章帶你搞懂Python類(lèi)的相關(guān)知識(shí)
今天我們要說(shuō)的是面向?qū)ο蟮暮诵?----類(lèi),類(lèi)能幫我們把復(fù)雜的事情變得有條理,有順序,希望大家通過(guò)學(xué)習(xí)類(lèi)能改善自己的編碼風(fēng)格,使代碼變得更為好看,更加通俗易懂,需要的朋友可以參考下2021-05-05python圖形開(kāi)發(fā)GUI庫(kù)pyqt5的基本使用方法詳解
這篇文章主要介紹了python圖形開(kāi)發(fā)GUI庫(kù)pyqt5的基本使用方法詳解,需要的朋友可以參考下2020-02-02