使用Python实现多线程和多处理

时间:2019-04-05 07:56:45

标签: python python-3.x multithreading optimization multiprocessing

我已经在Python中定义了一个(测试)函数,我正在使用该函数来理解执行代码可能需要的不同计算时间-使用普通代码(不使用多处理或多线程),以及然后一个接一个地实现它们。

功能(用于基本用法):

from random import randint as rInt

def highComputationFunction(rangeNumber):
    count_ = 0
    for i in range(rangeNumber):
        count_ = count_*2 + rInt(rangeNumber**2, rangeNumber**3)
    count_ = 10**100//count_

    return count_

此外,对于多处理和多线程,我想将线程的结果返回给我的父函数,因此需要进行如下修改:

from random import randint as rInt

def highComputationFunction(rangeNumber, result):
    count_ = 0
    for i in range(rangeNumber):
        count_ = count_*2 + rInt(rangeNumber**2, rangeNumber**3)
    count_ = 10**100//count_

    return count_

查看每个主要功能的CPU使用情况,如下所示:

import time

if __name__ == '__main__':
    startTime = time.time()
    rangeNumber = 10000
    coumputedNum = float(round(highComputationFunction(rangeNumber)//100**5000, 3))
    print('\tFunction of {} Executed in: {} seconds. Result = {}'.format(rangeNumber, round(time.time() - startTime, 2), coumputedNum))

    inTime = time.time()
    rangeNumber = 100000
    coumputedNum = float(round(highComputationFunction(rangeNumber)//100**5000, 3))
    print('\tFunction of {} Executed in: {} seconds. Result = {}'.format(rangeNumber, round(time.time() - inTime, 2), coumputedNum))

    inTime = time.time()
    rangeNumber = 1000000
    coumputedNum = float(round(highComputationFunction(rangeNumber)//100**5000, 3))
    print('\tFunction of {} Executed in: {} seconds. Result = {}'.format(rangeNumber, round(time.time() - inTime, 2), coumputedNum))

    print('Total Execution Time: {}'.format(round(time.time() - startTime, 2)))

总共执行了大约46秒。输出如下:

    # python understandComputation.py
    #       Function of 10000 Executed in: 0.03 seconds. Result = 0.0
    #       Function of 100000 Executed in: 0.91 seconds. Result = 0.0
    #       Function of 1000000 Executed in: 45.49 seconds. Result = 0.0
    # Total Execution Time: 46.44

使用多线程执行相同的操作:

import time
import threading

if __name__ == '__main__':
    startTime = time.time()
    result_ = 0
    threadList = []
    for i in [10000, 100000, 1000000]:
        curThread = threading.Thread(target = highComputationFunction, args = (i, result_))
        curThread.start()
        print('\tThread for {} Started.'.format(i))
        threadList.append(curThread)
        result_ += result_

    for i in threadList:
        i.join()

    print('Total Function Executed in: {} seconds. Result = {}'.format(round(time.time() - startTime, 2), result_))

对于多处理:

import time
import multiprocessing

if __name__ == '__main__':
    startTime = time.time()
    result_ = 0
    procList = []
    for i in [10000, 100000, 1000000]:
        curProc = multiprocessing.Process(target = highComputationFunction, args = (i, result_))
        curProc.start()
        print('\tProcess for {} Started.'.format(i))
        procList.append(curProc)
        result_ += result_

    for i in procList:
        i.join()

    print('Total Function Executed in: {} seconds. Result = {}'.format(round(time.time() - startTime, 2), result_))

实现这一点,比平时花费更多的时间。

    # python understandComputation.py
    #       Thread for 10000 Started.
    #       Thread for 100000 Started.
    #       Thread for 1000000 Started.
    # Total Function Executed in: 47.04 seconds. Result = 0

    # python understandComputation.py
    #       Process for 10000 Started.
    #       Process for 100000 Started.
    #       Process for 1000000 Started.
    # Total Function Executed in: 47.21 seconds. Result = 0

请告诉我代码执行是否错误。多线程和多处理的预期结果应小于45.5秒,这是在实际代码中执行1000000数字所花费的最长时间,但我没有得到所需的输出。

0 个答案:

没有答案