gpt4 book ai didi

python - 通过Asyncio解决阻塞http python函数

转载 作者:太空宇宙 更新时间:2023-11-03 14:24:21 27 4
gpt4 key购买 nike

我正在实现一个通过 http 请求进行通信的区 block 链 ( inspired by this blogpost )。该区 block 链具有工作量证明方法,根据难度,可以在相当长的时间内阻止其他 http 请求。这就是为什么我尝试从 python 实现新的 asyncio 功能。作品如下:

async def proof_of_work(self, last_proof):
"""
Simple Proof of Work Algorithm:
- Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
"""
proof = 0
while self.valid_proof(last_proof, proof) is False:
proof += 1
await asyncio.sleep(1)

return proof

但是,这使得我的工作量证明极其缓慢,我猜这是因为它在每次迭代后被迫休眠。解决这个问题的更优雅的方法是什么?

    while self.valid_proof(last_proof, proof) is False:
proof += 1
if proof % 1000 == 0:
await asyncio.sleep(1)

可以加快速度,但看起来有点脏。实现这个的正确方法是什么?

最佳答案

如果您想在协程内运行 CPU 阻塞代码,则应使用 run_in_executor() 在单独的执行流中运行它(以避免 asyncio 的事件循环卡住)。

如果您只是想要另一个执行流,或者(我认为更好)使用ProcessPoolExecutor将与CPU相关的工作委托(delegate)给其他核心,则可以使用ThreadPoolExecutor

import asyncio
from concurrent.futures import ProcessPoolExecutor
import hashlib


# ORIGINAL VERSION:
# https://github.com/dvf/blockchain/blob/master/blockchain.py
def valid_proof(last_proof, proof):
guess = f'{last_proof}{proof}'.encode()
guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:4] == "0000"


def proof_of_work(last_proof):
proof = 0
while valid_proof(last_proof, proof) is False:
proof += 1
return proof


# ASYNC VERSION:
async def async_proof_of_work(last_proof):
proof = await loop.run_in_executor(_executor, proof_of_work, last_proof)
return proof


async def main():
proof = await async_proof_of_work(0)
print(proof)


if __name__ == '__main__':
_executor = ProcessPoolExecutor(4)

loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()

输出:

69732

关于python - 通过Asyncio解决阻塞http python函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com