亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

#!/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ù),
    功能:使用多線程運行函數(shù),函數(shù)的參數(shù)只有一個file,并且未實現(xiàn)結果值的返回
    args:
      filelist  函數(shù)的參數(shù)為列表格式,
      funname  函數(shù)的名字為字符串,函數(shù)僅有一個參數(shù)為file
      delay   每個線程之間的延遲,
      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):
  '''''
  多進程運行函數(shù),多進程多線程運行函數(shù)
  args:
    filelist  函數(shù)的參數(shù)為列表格式,
    funname  函數(shù)的名字為字符串,函數(shù)僅有一個參數(shù)為file
    delay   每個線程\進程之間的延遲,
    max_threads 最大的線程數(shù)
    max_multiprocess 最大的進程數(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):
      '''''
    只使用多進程
      '''
      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))
    '''''
    測試使用5線程
    '''
    st = time.clock()
    asc = MyTMultithread(a,0,'func1',5)
    asc.startrun()
    end = time.clock()
    print('*'*50)
    print('多線程使用時間:',end-st)
    #測試使用5個進程
    st = time.clock()
    asd = Mymultiprocessing(a,0,'func1',5)
    asd.multiprocessingOnly()
    end = time.clock()
    print('*'*50)
    print('多進程使用時間:',end-st)
    #測試使用5進程10線程
    st = time.clock()
    multiPT = Mymultiprocessing(a,0,'func1',5,10)
    multiPT.multiprocessingThreads()
    end = time.clock()
    print('*'*50)
    print('多進程多線程使用時間:',end-st)

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

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

相關文章

  • Python環(huán)境管理virtualenv&virtualenvwrapper的配置詳解

    Python環(huán)境管理virtualenv&virtualenvwrapper的配置詳解

    這篇文章主要介紹了Python環(huán)境管理virtualenv&virtualenvwrapper的配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • pycharm debug功能實現(xiàn)跳到循環(huán)末尾的方法

    pycharm debug功能實現(xiàn)跳到循環(huán)末尾的方法

    今天小編就為大家分享一篇pycharm debug功能實現(xiàn)跳到循環(huán)末尾的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • 解決Python 中JSONDecodeError: Expecting value: line 1 column 1 (char 0)錯誤

    解決Python 中JSONDecodeError: Expecting value:&n

    這篇文章主要介紹了解決Python 中JSONDecodeError: Expecting value: line 1 column 1 (char 0)錯誤問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Python中迭代器與生成器的用法

    Python中迭代器與生成器的用法

    這篇文章介紹了Python中迭代器與生成器的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • 如何用python獲取到照片拍攝時的詳細位置(附源碼)

    如何用python獲取到照片拍攝時的詳細位置(附源碼)

    其實我們平時拍攝的照片里,隱藏了大量的信息,下面這篇文章主要給大家介紹了關于如何用python獲取到照片拍攝時的詳細位置,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-12-12
  • Python的列表和元組詳情

    Python的列表和元組詳情

    這篇文章主要介紹了Python的列表和元組,列表和元組是python組常見的內(nèi)置內(nèi)省,下面文章我們講圍繞Python的列表和元組的相關資料展開話題,感興趣的小伙伴以參考一下
    2021-10-10
  • python批量識別圖片指定區(qū)域文字內(nèi)容

    python批量識別圖片指定區(qū)域文字內(nèi)容

    這篇文章主要為大家詳細介紹了python識別圖片指定區(qū)域文字內(nèi)容,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • python如何刪除列為空的行

    python如何刪除列為空的行

    在本篇文章里小編給大家整理的是關于python刪除列為空的行方法,對此有需要的朋友們可以學習下。
    2020-07-07
  • python實現(xiàn)線性回歸算法

    python實現(xiàn)線性回歸算法

    這篇文章主要為大家詳細介紹了python實現(xiàn)線性回歸算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • python中pip安裝、升級以及升級固定的包

    python中pip安裝、升級以及升級固定的包

    我們知道python有大量的第三方庫,這也是python的優(yōu)勢之一,pip就是python整的軟件包管理系統(tǒng),類似于Linux平臺的yum倉庫,下面這篇文章主要給大家介紹了關于python中pip安裝、升級以及升級固定包的相關資料,需要的朋友可以參考下
    2022-02-02

最新評論