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

Python實(shí)現(xiàn)的自定義多線程多進(jìn)程類示例

 更新時(shí)間:2018年03月23日 08:50:55   作者:Magicapprentice  
這篇文章主要介紹了Python實(shí)現(xiàn)的自定義多線程多進(jìn)程類,結(jié)合實(shí)例形式分析了Python多線程多進(jìn)程的相關(guān)調(diào)用與使用操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)的自定義多線程多進(jìn)程類。分享給大家供大家參考,具體如下:

最近經(jīng)常使用到對(duì)大量文件進(jìn)行操作的程序以前每次寫的時(shí)候都要在函數(shù)中再寫一個(gè)多線程多進(jìn)程的函數(shù),做了些重復(fù)的工作遇到新的任務(wù)時(shí)還要重寫,因此將多線程與多進(jìn)程的一些簡(jiǎn)單功能寫成一個(gè)類,方便使用。功能簡(jiǎn)單只為以后方便使用。

使用中發(fā)現(xiàn)bug會(huì)再進(jìn)行更新

#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  # @Time  : 2017/5/10 12:47
  # @Author : zhaowen.zhu
  # @Site  :
  # @File  : MultiThread.py
  # @Software: Python Idle
  import threading,time,sys,multiprocessing
  from multiprocessing import Pool
  class MyTMultithread(threading.Thread):
    '''''
    自定義的線程函數(shù),
    功能:使用多線程運(yùn)行函數(shù),函數(shù)的參數(shù)只有一個(gè)file,并且未實(shí)現(xiàn)結(jié)果值的返回
    args:
      filelist  函數(shù)的參數(shù)為列表格式,
      funname  函數(shù)的名字為字符串,函數(shù)僅有一個(gè)參數(shù)為file
      delay   每個(gè)線程之間的延遲,
      max_threads 線程的最大值
    '''
    def __init__(self,filelist,delay,funname,max_threads = 50):
      threading.Thread.__init__(self)
      self.funname = funname
      self.filelist = filelist[:]
      self.delay = delay
      self.max_threads = max_threads
    def startrun(self):
      def runs():
        time.sleep(self.delay)
        while True:
          try:
            file = self.filelist.pop()
          except IndexError as e:
            break
          else:
            self.funname(file)
      threads = []
      while threads or self.filelist:
        for thread in threads:
          if not thread.is_alive():
            threads.remove(thread)
        while len(threads) < self.max_threads and self.filelist:
          thread = threading.Thread(target = runs)
          thread.setDaemon(True)
          thread.start()
          threads.append(thread)
  class Mymultiprocessing (MyTMultithread):
  '''''
  多進(jìn)程運(yùn)行函數(shù),多進(jìn)程多線程運(yùn)行函數(shù)
  args:
    filelist  函數(shù)的參數(shù)為列表格式,
    funname  函數(shù)的名字為字符串,函數(shù)僅有一個(gè)參數(shù)為file
    delay   每個(gè)線程\進(jìn)程之間的延遲,
    max_threads 最大的線程數(shù)
    max_multiprocess 最大的進(jìn)程數(shù)
  '''
    def __init__(self,filelist,delay,funname,max_multiprocess = 1,max_threads = 1):
      self.funname = funname
      self.filelist = filelist[:]
      self.delay = delay
      self.max_threads = max_threads
      self.max_multiprocess = max_multiprocess
      self.num_cpus = multiprocessing.cpu_count()
    def multiprocessingOnly(self):
      '''''
    只使用多進(jìn)程
      '''
      num_process = min(self.num_cpus,self.max_multiprocess)
      processes = []
      while processes or self.filelist:
        for p in processes:
          if not p.is_alive():
            # print(p.pid,p.name,len(self.filelist))
            processes.remove(p)
        while len(processes) < num_process and self.filelist:
          try:
            file = self.filelist.pop()
          except IndexError as e:
            break
          else:
            p = multiprocessing.Process(target=self.funname,args=(file,))
            p.start()
            processes.append(p)
    def multiprocessingThreads(self):
      num_process = min(self.num_cpus,self.max_multiprocess)
      p = Pool(num_process)
      DATALISTS = []
      tempmod = len(self.filelist) % (num_process)
      CD = int((len(self.filelist) + 1 + tempmod)/ (num_process))
      for i in range(num_process):
        if i == num_process:
          DATALISTS.append(self.filelist[i*CD:-1])
        DATALISTS.append(self.filelist[(i*CD):((i+1)*CD)])
      try:
        processes = []
        for i in range(num_process):
          #print('wait add process:',i+1,time.clock())
          #print(eval(self.funname),DATALISTS[i])
          MultThread = MyTMultithread(DATALISTS[i],self.delay,self.funname,self.max_threads)
          p = multiprocessing.Process(target=MultThread.startrun())
          #print('pid & name:',p.pid,p.name)
          processes.append(p)
        for p in processes:
          print('wait join ')
          p.start()
        print('waite over')
      except Exception as e:
        print('error :',e)
      print ('end process')
  def func1(file):
    print(file)
  if __name__ == '__main__':
    a = list(range(0,97))
    '''''
    測(cè)試使用5線程
    '''
    st = time.clock()
    asc = MyTMultithread(a,0,'func1',5)
    asc.startrun()
    end = time.clock()
    print('*'*50)
    print('多線程使用時(shí)間:',end-st)
    #測(cè)試使用5個(gè)進(jìn)程
    st = time.clock()
    asd = Mymultiprocessing(a,0,'func1',5)
    asd.multiprocessingOnly()
    end = time.clock()
    print('*'*50)
    print('多進(jìn)程使用時(shí)間:',end-st)
    #測(cè)試使用5進(jìn)程10線程
    st = time.clock()
    multiPT = Mymultiprocessing(a,0,'func1',5,10)
    multiPT.multiprocessingThreads()
    end = time.clock()
    print('*'*50)
    print('多進(jìn)程多線程使用時(shí)間:',end-st)

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

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

相關(guān)文章

  • Jupyter?Notebook的使用詳解

    Jupyter?Notebook的使用詳解

    JupyterNotebook是一個(gè)強(qiáng)大的工具,可以用于數(shù)據(jù)科學(xué)和機(jī)器學(xué)習(xí)等領(lǐng)域的各種任務(wù),該文章介紹了JupyterNotebook的基本使用方法,包括新建和保存Notebook,插入圖像,運(yùn)行代碼,以及許多快捷鍵的使用
    2025-01-01
  • Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析

    Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)的KMeans聚類算法,結(jié)合實(shí)例形式較為詳細(xì)的分析了KMeans聚類算法概念、原理、定義及使用相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • Python?PySpark案例實(shí)戰(zhàn)教程

    Python?PySpark案例實(shí)戰(zhàn)教程

    PySpark是由Spark官方開發(fā)的Python語(yǔ)言第三方庫(kù),Python開發(fā)者可以使用pip程序快速的安裝PySpark并像其它三方庫(kù)那樣直接使用,本文給大家介紹Python?PySpark案例實(shí)戰(zhàn),感興趣的朋友一起看看吧
    2023-09-09
  • 如何獲取numpy array前N個(gè)最大值

    如何獲取numpy array前N個(gè)最大值

    這篇文章主要介紹了獲取numpy array前N個(gè)最大值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 解決Python3 控制臺(tái)輸出InsecureRequestWarning問(wèn)題

    解決Python3 控制臺(tái)輸出InsecureRequestWarning問(wèn)題

    這篇文章主要介紹了解決Python3 控制臺(tái)輸出InsecureRequestWarning的問(wèn)題 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python錯(cuò)誤NameError:name?'X'?is?not?defined的解決方法

    Python錯(cuò)誤NameError:name?'X'?is?not?defined的解決方法

    這篇文章主要給大家介紹了關(guān)于Python錯(cuò)誤NameError:name?‘X‘?is?not?defined的解決方法,這是最近工作中遇到的一個(gè)問(wèn)題,文中通過(guò)實(shí)例代碼將解決的方法介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • python 實(shí)現(xiàn)批量文件加密功能

    python 實(shí)現(xiàn)批量文件加密功能

    python自動(dòng)化辦公現(xiàn)在可不是一個(gè)陌生的詞,也隨著人們對(duì)自己隱私越來(lái)越看重,本文主要介紹了python 實(shí)現(xiàn)批量文件加密功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Flask-Docs自動(dòng)生成Api文檔安裝使用教程

    Flask-Docs自動(dòng)生成Api文檔安裝使用教程

    這篇文章主要為大家介紹了Flask-Docs自動(dòng)生成Api文檔安裝使用教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • python打包成so文件過(guò)程解析

    python打包成so文件過(guò)程解析

    這篇文章主要介紹了python打包成so文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • python實(shí)現(xiàn)簡(jiǎn)易的學(xué)生信息管理系統(tǒng)

    python實(shí)現(xiàn)簡(jiǎn)易的學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易的學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論

霞浦县| 晋城| 资溪县| 贡觉县| 定边县| 天等县| 兴海县| 绍兴市| 山西省| 芦山县| 盈江县| 金溪县| 侯马市| 乌拉特后旗| 宜都市| 繁峙县| 福安市| 宣威市| 阜阳市| 天峨县| 佛教| 富锦市| 保靖县| 轮台县| 泰顺县| 潮安县| 通许县| 高雄市| 宜川县| 云龙县| 贵定县| 兴山县| 伊吾县| 元阳县| 江津市| 西乌珠穆沁旗| 山阴县| 陇南市| 三亚市| 页游| 潮州市|