我试图在python中编写一个简单的工作证明nonce-finder.
def proof_of_work(b, nBytes):
nonce = 0
# while the first nBytes of hash(b + nonce) are not 0
while sha256(b + uint2bytes(nonce))[:nBytes] != bytes(nBytes):
nonce = nonce + 1
return nonce
Run Code Online (Sandbox Code Playgroud)
现在我尝试进行多处理,因此它可以使用所有CPU内核并更快地找到nonce.我的想法是multiprocessing.Pool多次使用和执行函数proof_of_work,传递两个参数num_of_cpus_running,this_cpu_id如下所示:
def proof_of_work(b, nBytes, num_of_cpus_running, this_cpu_id):
nonce = this_cpu_id
while sha256(b + uint2bytes(nonce))[:nBytes] != bytes(nBytes):
nonce = nonce + num_of_cpus_running
return nonce
Run Code Online (Sandbox Code Playgroud)
所以,如果有4个核心,每个核心将计算这样的随机数:
core 0: 0, 4, 8, 16, 32 ...
core 1: 1, 5, 9, 17, 33 ...
core 2: 2, …Run Code Online (Sandbox Code Playgroud) 当我在Windows上使用此命令时:
python -m PyQt4.uic.pyuic user_interface.ui -o user_interface.py
Run Code Online (Sandbox Code Playgroud)
之后,我添加了一个资源:
pyrcc4.exe -py3 images.qrc -o images.py
Run Code Online (Sandbox Code Playgroud)
最后,我得到了两个漂亮的文件user_interface.py和images.py。问题是user_interface.py文件以以下代码结尾:
... all QT stuff here.
import images_re
Run Code Online (Sandbox Code Playgroud)
而且由于这是许多父母调用的模块,因此必须像这样导入:
import myapp.gui.images_re
Run Code Online (Sandbox Code Playgroud)
当我更改代码行时,它可以正常工作,但是每次我修改user_interface.ui文件然后执行批处理时,它将被覆盖,因此每次都必须手动对其进行更改。
有什么方法可以告诉pyuic在该import语句中写什么?
还是在pyuic之后可以执行并更改该行代码的任何批处理代码?
还是对调用user_interface.py的.py文件进行一些调整,例如更改默认目录,以便从那里导入images_re?
我有一本词典字典.我想算一下这些词典中有多少元素的"状态"设置为"连接".
这是我的工作代码:
connecting = 0
for x in self.servers:
if self.servers[x]["status"] == "connecting": connecting += 1
Run Code Online (Sandbox Code Playgroud)
有没有办法压缩这个?我想的是:
connecting = [1 if self.servers[x]["status"] == "closed" else 0 for x in self.servers]
Run Code Online (Sandbox Code Playgroud)
但它只返回0和1的列表,不会将1添加到连接,这是我的预期.