Python 多進程與多線程優化

Python 多進程與多線程優化

Python 多線程代碼

from time import ctime, sleep
import threading
import numpy as np
import collections
  
loops = [1e6,1e7]
class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.func = func
        self.args = args
        self.result = self.func(*self.args)
  
    def get_result(self):
        try:
            return self.result
        except Exception:
            return None
  
def loop(num):
    sum = 0
    for i in range(1,num+1):
        sum = sum + i
    return sum
 
def main():
    print('Start:', ctime())
    threads = []
    nloops = range(len(loops))
    for i in nloops:
        t = MyThread(loop, (int(loops[i]),), loop.__name__)
        threads.append(t)
    for i in nloops:   # start threads 此處並不會執行線程,而是將任務分發到每個線程,同步線程。等同步完成後再開始執行start方法
        threads[i].start()
    for i in nloops:   # jion()方法等待線程完成
        threads[i].join()
    for i in nloops:   # jion()方法等待線程完成
        print(threads[i].get_result())
    print('Done:', ctime())
  
 
if __name__ == '__main__':
    main()

Python 多進程代碼

import multiprocessing
import time
 
def loop(num):
    sum = 0
    for i in range(1,num+1):
        sum = sum + i
    return sum
 
if __name__ == '__main__':
    print('Start:', ctime())
    pool = multiprocessing.Pool(multiprocessing.cpu_count())
    result = []
    loops = [1e6,1e7]
    for i in range(len(loops)):
        result.append(pool.apply_async(loop, (int(loops[i]), )))
    pool.close()
    pool.join()
    for res in result:
        print(res.get())
    print('Done:', ctime())

Python 線程池和進程池

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
  
loops = [1e6,1e7]
  
def loop(num,num1):
    sum = 0
    for i in range(1,num+1):
        sum = sum + i
    return sum
 
def main():
    print('Start:', ctime())
    tasks = []
    nloops = range(len(loops))
    executor = ThreadPoolExecutor(max_workers=8) # ProcessPoolExecutor(8)
    for i in nloops:
        tasks.append(executor.submit(loop, int(loops[i]),5))
    for i in nloops:   # start threads 此處並不會執行線程,而是將任務分發到每個線程,同步線程。等同步完成後再開始執行start方法
        print(tasks[i].result())
    print('Done:', ctime())
  
 
if __name__ == '__main__':
    main()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章