简体   繁体   English

多线程 Python 脚本但只有 20% 的 CPU? 如何加快我的 Python 脚本?

[英]Multithreaded Python Script but just 20% CPU? How can I speed up my Python Script?

I programmed a simple SHA256 blockchain mining algorithm in python using hashlib.我使用 hashlib 在 python 中编写了一个简单的 SHA256 区块链挖掘算法。 What I am trying to do here is changing a random number in my data and then calculating the SHA256 hash.我在这里要做的是更改数据中的随机数,然后计算 SHA256 hash。 Whenever the hash has the predefined amount of zeroes in the beginning, it prints the hash and its corresponding nonce to the console.每当 hash 在开头有预定义数量的零时,它就会将 hash 及其对应的 nonce 打印到控制台。 The goal is to have 11 zeroes in the beginning.目标是在开始时有 11 个零。

import hashlib
import threading

#bn=block number d=input data, ph=previous hash
bn = 1

d = "mydata"

ph = "0000000000000000000000000000000000000000000000000000000000000000"


class MyThread(threading.Thread):
    def __init__(self, threadID, begin, end):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.end = end
        self.begin = begin


    def run(self):
        for val in range(self.begin, self.end):
            mine(self.threadID, val)
        print("done " + str(self.threadID))

#hashing function

def mine(id, x):
    hash = hashlib.sha256((str(bn) + str(x) + str(d) + str(ph)).encode('utf-8')).hexdigest()
    if hash.find("0000000", 0, 7) > -1:
        # print(id)
        print(x)
        print(hash)

#now break it up and distribute it to the threads

#Possible SHA256 Combinations 8388608000000

pc = 8388608000000

#Number of desired Threads (more threads help more?)

nt = 704

#calculating the steps

step = round((pc)/(nt))

#starting the threads with each thread calculating a different range of nonces.
for x in range (1, nt):
    thread = MyThread((x), (x-1)*(step), (x)*(step))
    thread.start()

How can I speed this thing up?我怎样才能加快这件事?

Currently, I run this on an Intel 8250u quadcore and I can get hashes with 7 zeroes in the beginning comfortably.目前,我在 Intel 8250u 四核上运行它,我可以在开始时轻松地获得 7 个零的哈希值。 However, 8 zeroes already take 5 hours.但是,8 个零已经需要 5 个小时。 Funnily, only around 20-30% of my CPU is used.有趣的是,我只使用了大约 20-30% 的 CPU。 How can I make it run on all cores with threads running in parallel?如何让它在所有内核上运行,线程并行运行?

Do you have another Idea how I can speed this thing up?你有另一个想法我可以如何加快这件事吗? I have better hardware (Threadripper 1920) available but I'm afraid that alone will not give me a 300x - 4000x (best case) speed increase...我有更好的硬件(Threadripper 1920)可用,但恐怕仅此一项不会给我带来 300 倍 - 4000 倍(最佳情况)的速度提升......

My first thought was outsourcing this to the GPU since I know that Bitcoin Mining evolved from CPU to GPU.我的第一个想法是将其外包给 GPU,因为我知道比特币挖矿从 CPU 发展到 GPU。 When looking into Numba I saw that it does not support Hashlib.在查看 Numba 时,我发现它不支持 Hashlib。 I also saw that compiling this using Pypy might speed it up?我还看到使用 Pypy 编译它可能会加快速度? What approach would you guys favour?你们喜欢什么方法? I'm confused...我很困惑...

Thanks for being patient with me谢谢你对我的耐心

Python is a bit weird in this regard. Python 在这方面有点奇怪。 What you're doing here is multithreading and not multiprocessing.你在这里做的是多线程而不是多处理。 Because of the Global Interpreter Lock, these threads aren't actually running at the same time.由于全局解释器锁,这些线程实际上并没有同时运行。 Check out the multiprocessing module for Python if you want to actually run calculations in parallel.如果您想实际并行运行计算,请查看Python 的多处理模块

EDIT: As Jared mentioned, Bitcoin mining on your personal computer is no longer profitable due to most mining now being done with specialized hardware .编辑:正如 Jared 所提到的,由于现在大多数挖矿都是使用专用硬件完成的,因此在您的个人计算机上进行比特币挖矿不再有利可图。 As a project this is cool, but I wouldn't expect it to make you money.作为一个项目,这很酷,但我不希望它能让你赚钱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM