How to make this task improve cpu usage?

Viewed 48

I try to hash many file, but it not use full of cpu power. it only consume 25%. i test to move the heavy process into thread. but still no different. im from nodejs use sharp library. with same task. it consume all cpu usage. How python to make it full power?

import cv2
import math
import datetime
import hashlib
import threading

def thread_function(image, yPos, xPos, wSizeBlock, hSizeBlock):
    block = image[yPos:yPos+wSizeBlock, xPos:xPos+hSizeBlock]

    hash = hashlib.sha256()
    hash.update(block.tobytes())
    print(hash.hexdigest())

image = cv2.imread('frame323.jpg', cv2.IMREAD_COLOR)
dimension = {
    'width': image.shape[1],
    'height': image.shape[0]
}

wSizeBlock = int(16)
hSizeBlock = int(16)
wBlockLength = math.floor(dimension['width'] / wSizeBlock)
hBlockLength = math.floor(dimension['height'] / hSizeBlock)
count = 0

start_time = datetime.datetime.now()
print(start_time)
for k in range(0, 500):
    for i in range(0, wBlockLength):
        for j in range(0, hBlockLength):
            xPos = int(i*wSizeBlock)
            yPos = int(j*hSizeBlock)

            x = threading.Thread(target=thread_function, args=(image, xPos, yPos, wSizeBlock, hSizeBlock))
            x.start()
            count += 1
    count = 0

end_time = datetime.datetime.now()
print(end_time)
1 Answers

For CPU intensive operations that can be split up into smaller tasks, you would want to use the multiprocessing module. It is similar to the threading module in that it allows multiple functions to be ran at once. Syntax looks something like this:

import multiprocessing as mp


def add(a, b):
    return a + b


p = mp.Process(target=add, args=(1, 2))
p.start()
Related