关于 steem-python 库的 AttibuteError 异常分析
用 steem-python 库的时候经常会碰到下面的异常, 一行一行的分析, 以前我一直没时间也懒得追究, 今天看了看 steem-python 的源码, 其实异常产生的原因很简单.
我们就以下面的异常为例, 首先是请求的节点不稳定, 可能负荷过重, 可能是 upstream 有 bug 进程挂了, 总之不能正常处理我们的请求而返回了 502 错误; 然后 steem-python 代码里发现返回了 502, 于是 self.info()
返回了 None, 于是下面这句 self.info().get(self.mode)
就发生了 AttributeError
异常.
2017-11-11 10:29:16 non 200 response:502
2017-11-11 10:29:16 failed to load response
Traceback (most recent call last):
File "/home/dc2-user/steem/main.py", line 150
head_block = self.get_current_block_num()
File "/usr/local/lib/python3.5/dist-packages/steem/blockchain.py", line 40, in get_current_block_num
return self.info().get(self.mode)
AttributeError: 'NoneType' object has no attribute 'get'
steem-python 其实支持指定多个备用节点, 当其中一个节点 down 了的时候自动尝试下一个节点, 但是实现的不好, 有两个主要的问题:
- 每次请求都从第一个节点开始尝试, 而在某段时间内如果第一个节点就是有问题, 显然会造成很多不必要的请求失败
- 返回 502 时没有进行重试, 这点有点坑爹...
了解了这两点之后, 也就知道怎么改进这个库了.. 首先得自己负责维护节点列表并记录节点的健康状态, 对于某段时间不健康的节点就直接放到队尾去, 另外就是碰到 502 的情况也要进行重试.
希望改进了这两点后能够更稳定写吧..
其它文章
数字货币
- 什么是比特币的链上 (on-chain) 与链下 (off-chain) 交易, 以及往交易所充币后发生了什么
- 为何留在 steemit?
- 说一说重放, 重放保护, 以及分叉期间我们该怎么做
- 闲聊即将到来的 segwit2x 分叉
- electrum 钱包的 sweep 功能小记
- 浅读 Steemit 的设计与规则
- bitshares 中的账户与权限个人理解
- 比特币地址, 公钥与私钥的格式以及如何保证比特币不丢失
Congratulations @cifer! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Congratulations @cifer! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP