关于 steem-python 库的 AttibuteError 异常分析

in #cn7 years ago (edited)

用 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 了的时候自动尝试下一个节点, 但是实现的不好, 有两个主要的问题:

  1. 每次请求都从第一个节点开始尝试, 而在某段时间内如果第一个节点就是有问题, 显然会造成很多不必要的请求失败
  2. 返回 502 时没有进行重试, 这点有点坑爹...

了解了这两点之后, 也就知道怎么改进这个库了.. 首先得自己负责维护节点列表并记录节点的健康状态, 对于某段时间不健康的节点就直接放到队尾去, 另外就是碰到 502 的情况也要进行重试.

希望改进了这两点后能够更稳定写吧..


其它文章

数字货币

数据分析

交易所

编程系列

Sort:  

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

By upvoting this notification, you can help all Steemit users. Learn how here!

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

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 58661.64
ETH 2723.56
USDT 1.00
SBD 2.32