hystrix启发的延迟和容错库。
pycopine的Python项目详细描述
pycopine是一个延迟和容错库,用于隔离 访问远程系统、服务和第三方库,停止级联 在复杂的分布式系统中,当发生故障时 是不可避免的。
正如这篇文章所暗示的,皮科平深受 Hystrix。
先决条件
pycopine需要python 3.2+,但将来可能会被移植到2.7。
安装
Pycopine还没有发布。
(计划)功能
- 检测并报告失败的服务。
- 短路服务,在高负荷下故障,以帮助他们恢复。
- 监控故障率和性能指标以检测瓶颈。
- 在运行时,从任何地方按需管理线程池和队列大小。
- …(更多内容)
示例
假设我们要与速度慢、不可靠或两者兼而有之的远程服务通话:
importtimeimportrandomdefcrappy_service(input):''' The most useless piece of code ever.'''time.sleep(5)if'OK'!=random.choice(['OK','OK','f**ck']):raiseRuntimeError('We broke something.')returninput
你可以在这个问题上抛出很多线程和try/except子句 不要破坏互联网。或者您可以使用pycopine:
frompycopineimportCommandclassMyCommand(Command):''' Does nothing with the input, but with style. '''defrun(self,input):returncrappy_service(input)deffallback(self,input):returninput# Run and wait for the resultresult=MyCommand('input').result()# Give up after 2 secondsresult=MyCommand('input').result(timeout=2)# Fire and forgetMyCommand('input').submit()# Do stuff in parallelfoo=MyCommand('input_a').submit()bar=MyCommand('input_b').submit()results=[foo.result(),bar.result()]# Change your mind midway throughfoobar=MyCommand('input').submit()iffoobar.wait(timeout=2):result=foobar.reault()else:foobar.cancel(RuntimeError('No time for this sh**t'))