最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python開發(fā)之基于thread線程搜索本地文件的方法

 更新時間:2015年11月11日 09:45:04   作者:Hongten  
這篇文章主要介紹了python開發(fā)之基于thread線程搜索本地文件的方法,以完整實例形式分析了Python基于多線程處理搜索問題的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了python開發(fā)之基于thread線程搜索本地文件的方法。分享給大家供大家參考,具體如下:

先來看看運行效果圖:

利用多個線程處理搜索的問題,我們可以發(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()

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • python取均勻不重復(fù)的隨機數(shù)方式

    python取均勻不重復(fù)的隨機數(shù)方式

    今天小編就為大家分享一篇python取均勻不重復(fù)的隨機數(shù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python的random和time模塊詳解

    python的random和time模塊詳解

    這篇文章主要介紹了python的random和time模塊,具有一定借鑒價值,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-10-10
  • Python異步庫asyncio、aiohttp詳解

    Python異步庫asyncio、aiohttp詳解

    這篇文章主要介紹了Python異步庫asyncio、aiohttp使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python中requests做接口測試的方法

    Python中requests做接口測試的方法

    Requests是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務(wù)器響應(yīng)數(shù)據(jù)時經(jīng)常會用到,本文主要介紹了Python中requests做接口測試的方法,感興趣的可以了解一下
    2021-05-05
  • Python Web編程之WSGI協(xié)議簡介

    Python Web編程之WSGI協(xié)議簡介

    這篇文章主要介紹了Python Web編程之WSGI協(xié)議,簡單說明了WSGI的概念、功能并結(jié)合實例形式分析了Gunicorn和uWSGI相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2018-07-07
  • Python3.5實現(xiàn)的三級菜單功能示例

    Python3.5實現(xiàn)的三級菜單功能示例

    這篇文章主要介紹了Python3.5實現(xiàn)的三級菜單功能,涉及Python針對json格式數(shù)據(jù)的讀取、遍歷、查找、判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03
  • 5款實用的python 工具推薦

    5款實用的python 工具推薦

    工欲善其事必先利其器,一個好的工具能讓起到事半功倍的效果,Python 社區(qū)提供了足夠多的優(yōu)秀工具來幫助開發(fā)者更方便的實現(xiàn)某些想法,下面這幾個工具給我的工作也帶來了很多便利,推薦給追求美好事物的你。
    2020-10-10
  • Django model.py表單設(shè)置默認(rèn)值允許為空的操作

    Django model.py表單設(shè)置默認(rèn)值允許為空的操作

    這篇文章主要介紹了Django model.py表單設(shè)置默認(rèn)值允許為空的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python可視化神器pyecharts繪制水球圖

    Python可視化神器pyecharts繪制水球圖

    這篇文章主要介紹了Python可視化神器pyecharts繪制水球圖,水球圖首先是動態(tài)的效果,像水流一樣波動,所以看起來比較的舒服,一般用于業(yè)務(wù)里面的完成率,其實和之前的儀表盤有點類似
    2022-07-07
  • Python爬蟲DOTA排行榜爬取實例(分享)

    Python爬蟲DOTA排行榜爬取實例(分享)

    下面小編就為大家?guī)硪黄狿ython爬蟲DOTA排行榜爬取實例(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06

最新評論

洛扎县| 和平区| 和田县| 涪陵区| 东山县| 安西县| 板桥市| 东阿县| 卢湾区| 华容县| 花垣县| 双桥区| 饶河县| 二连浩特市| 隆安县| 专栏| 岐山县| 昭苏县| 新邵县| 巩义市| 双辽市| 基隆市| 公安县| 海淀区| 井陉县| 娄底市| 隆林| 惠东县| 山东省| 晋州市| 高安市| 油尖旺区| 潞西市| 贵南县| 安乡县| 恭城| 辉县市| 桂阳县| 西宁市| 大英县| 怀安县|