如果internet连接丢失,如何重置python程序
好的,我用Python编写了这个程序,运行在Python 2.7上,我用它来交换BTC。在编程方面,我是一个完全的新手,但我对如何调整bot和为其创建规则非常了解。它运行正常,直到互联网连接发生变化,即打开/关闭VPN。我想知道如果无法获得响应,我可以使用什么代码重新启动程序?谢谢你的帮助。下面是用于启动程序和主循环的代码如果internet连接丢失,如何重置python程序,python,Python,好的,我用Python编写了这个程序,运行在Python 2.7上,我用它来交换BTC。在编程方面,我是一个完全的新手,但我对如何调整bot和为其创建规则非常了解。它运行正常,直到互联网连接发生变化,即打开/关闭VPN。我想知道如果无法获得响应,我可以使用什么代码重新启动程序?谢谢你的帮助。下面是用于启动程序和主循环的代码 def loop_body(self): orders = self.update_portfolio() if orders is No
def loop_body(self):
orders = self.update_portfolio()
if orders is None:
return
if self.get_num_open_bids(orders) + self.get_num_open_asks(orders) >= MAX_OPEN_ORDERS and REMOVE_UNREALISTIC:
self.update_portfolio
if self.get_num_open_bids(orders) + self.get_num_open_asks(orders) >= MAX_OPEN_ORDERS:
if DEBUG_MODE:
print '---'
print 'Too many open orders, sleep for', TOO_MANY_OPEN_SLEEP, 'seconds.'
print " "
print 'I have', self.get_num_portfolio_bids(), 'open bids,', self.get_num_portfolio_asks(), 'asks.'
print 'API shows', self.get_num_open_bids(orders), 'open bids,', self.get_num_open_asks(orders), 'asks.'
print "---"
print 'Profit :', self.profit, 'CNY'
sleep(TOO_MANY_OPEN_SLEEP)
return
a = None
b = None
d = None
e = None
market_depth = self.get_market_depth()
if not market_depth:
return
market_lowest_ask = self.get_lowest_market_ask(market_depth)
a = market_lowest_ask
market_highest_bid = self.get_highest_market_bid(market_depth)
d = market_highest_bid
sleep(5)
market_depth = self.get_market_depth()
if not market_depth:
return
market_lowest_ask = self.get_lowest_market_ask(market_depth)
b = market_lowest_ask
market_highest_bid = self.get_highest_market_bid(market_depth)
e = market_highest_bid
if DEBUG_MODE:
print '---'
print 'I have', self.get_num_portfolio_bids(), 'open bids,', self.get_num_portfolio_asks(), 'asks.'
print 'API shows', self.get_num_open_bids(orders), 'open bids,', self.get_num_open_asks(orders), 'asks.'
print "---"
print 'Profit :', self.profit, 'CNY'
my_ask_price_2 = market_lowest_ask - CNY_STEP
my_bid_price_2 = my_ask_price_2 - MIN_SURPLUS
if a > b and d > e:
for trial in xrange(MAX_TRIAL):
response = self.trader.sell('{0:.2f}'.format(my_ask_price_2), BTC_AMOUNT)
if response is True:
self.portfolio.append(
{'bid': my_bid_price_2, 'ask': my_ask_price_2, 'status': 'sell'})
if DEBUG_MODE:
print "---"
print 'I sold', BTC_AMOUNT, 'bitcoins at', my_ask_price_2
break
else:
if DEBUG_MODE:
print "---"
print 'Sell failed:', response
break
break
my_bid_price = market_highest_bid + CNY_STEP
my_ask_price = my_bid_price + MIN_SURPLUS
if a < b and d < e:
for trial in xrange(MAX_TRIAL):
if self.trader.buy('{0:.2f}'.format(my_bid_price), BTC_AMOUNT):
self.portfolio.append(
{'bid': my_bid_price, 'ask': my_ask_price, 'status': 'buy'})
if DEBUG_MODE:
print "---"
print 'I bought', BTC_AMOUNT, 'bitcoins at', my_bid_price
break
else:
if DEBUG_MODE:
print "---"
print 'Buy failed:', response
break
break
def start(self):
self.reset()
while True:
self.loop_body()
if __name__ == '__main__':
bot = Bot()
bot.start()
最好只是管理您的互联网交易,并检查请求是否超时。然后适当地处理这种情况——睡觉,再试一次,等等
如果要定期重新启动python程序,可能需要第二个脚本或shell脚本,但这并不是处理这种情况的正确方法。这里需要做两件事 首先,假设您所指的“未能获得响应的事件”是由Bot库引发的异常。因此,您需要以某种方式处理该异常 第二,您需要一个永远的循环,而这个循环围绕着所有内容,所以在处理异常之后,您可以返回顶部并重试 如果您在除Windows以外的任何平台上,并且不介意每次连接丢失时都打印出丑陋的异常回溯,那么在包装器外壳脚本中执行此操作可能更容易:
#!/bin/sh
while true; do
python ./myscript.py
done
无论程序是正常退出还是由于异常退出,shell脚本都将再次通过循环
如果要在Python中执行此操作,可以将顶级代码更改为:
if __name__ == '__main__':
while True:
try:
bot = Bot()
bot.start()
except Exception as e:
print('Failed with {!r}, retrying', e)
如果您确实知道在原始回溯中没有得到的特定异常,那么它将是新循环中的e类型,您可能希望仅处理该异常。这样,如果您的程序出现其他问题,它将向您显示出哪里出了问题,而不是永远循环。没有什么比打字错误更糟糕的了,它会导致无限循环的Failed with NameError:“slef”消息……一旦知道了,只需将except行更改为:
请看-这里的代码转储对于人们来说太长了,但也不包括任何连接代码或Bot的定义等相关部分。我假设Bot是他从某处获得的某个框架,它不知道如何处理重新连接。当然,正确的做法是通过子类化或分叉适当的类来扩展Bot框架,但我猜这超出了他现在想要学习的范围。但你当然是对的,那会好得多。你不仅可以更明智地处理这个问题,而且不必放弃整个当前状态,重新开始…
except LostConnectionException as e: