Python Forum
Multiprocessing - low CPU usage when more than 2 cores
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiprocessing - low CPU usage when more than 2 cores
#1
Hello,
I'm trying to understand how multiprocessing-multithreading works. Suppose I have the following code (which is basically the idea behind mining). In this case I am using 2 cores and everything looks good.
However if I choose to use more cores, the CPU utilization drops. i.e. if I use 4 cores, it utilizes them at 50% each. If I use 8, it utilizes them at 25% and so on.
My CPU is i9-9980HK 8-core.
Thank you

import hashlib
import time
import multiprocessing as mp
def hash_string(string):
    """
    Return a SHA-256 hash of the given string
    """
    return hashlib.sha256(string.encode('utf-8')).hexdigest()


def mine(seed,quit,foundit):
    #start = time.time()
    while not quit.is_set():
        test = hash_string(str(seed) + "Teststring")
        seed +=1
        if test[0:6] == '000000':
            print(test)
            print(seed)
            #print("Total time:" + str(time.time() - start))
            foundit.set()
            break

import multiprocessing as mp
quit = mp.Event()
foundit = mp.Event()
start = time.time()
#for i in range(int(mp.cpu_count()/2)):
for i in range(2):
    p = mp.Process(target=mine, args=((i+2)*100000000, quit, foundit))
    p.start()
foundit.wait()
quit.set()
print("Total time:" + str(time.time() - start))
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020