Python请求lib花费的时间比执行get请求的时间更长 [英] Python requests lib is taking way longer than it should to do a get request

查看:71
本文介绍了Python请求lib花费的时间比执行get请求的时间更长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这段代码.每当我运行代码并到达第3行时,大约需要20秒钟来完成get请求.没有理由要花费这么长时间,而且每次都持续花费很长时间.有帮助吗?

So I have this code. Whenever I run the code, and it gets to line 3, it takes about 20 whole seconds to do the get request. There is no reason it should be taking this long, and it's consistently taking long every time. Any help?

def get_balance(addr):
try:
    r = requests.get("http://blockexplorer.com/api/addr/"+addr+"/balance")
    return int(r.text)/10000000
except:
    return "e"

推荐答案

在大多数情况下,它对我有用.

It works for me most of the time.

>>> def get_balance(addr):
...   try:
...       start = time.time()
...       r = requests.get("http://blockexplorer.com/api/addr/"+addr+"/balance")
...       end = time.time()
...       print(f"took {end - start} seconds")
...       print(r.text, "satoshis")
...       return int(r.text)/100000000
...   except:
...       return "e"
...
>>>
>>> get_balance("1HB5XMLmzFVj8ALj6mfBsbifRoD4miY36v")
took 0.7754228115081787 seconds
151881086 satoshis
15.1881086

但是,如果我连续这样做足够多次,我偶尔会收到错误消息比特币JSON-RPC:超出工作队列深度.代码:429"

But if I do this enough times in a row, I'll occasionally get the error "Bitcoin JSON-RPC: Work queue depth exceeded. Code:429"

像我一样打印出r.text,这可能会向您显示来自Block Explorer的错误消息.可能是他们开始限制您的速率了.

Print out r.text like I did, and that might show you an error message from Block Explorer. It might be that they have started rate-limiting you.

这篇关于Python请求lib花费的时间比执行get请求的时间更长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆