本文整理汇总了Python中logs.Logs.catch方法的典型用法代码示例。如果您正苦于以下问题:Python Logs.catch方法的具体用法?Python Logs.catch怎么用?Python Logs.catch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logs.Logs
的用法示例。
在下文中一共展示了Logs.catch方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: twitter_callback
# 需要导入模块: from logs import Logs [as 别名]
# 或者: from logs.Logs import catch [as 别名]
def twitter_callback(tweet):
"""Analyzes Trump tweets, makes stock trades, and sends tweet alerts."""
# Initialize these here to create separate httplib2 instances per thread.
analysis = Analysis(logs_to_cloud=LOGS_TO_CLOUD)
trading = Trading(logs_to_cloud=LOGS_TO_CLOUD)
companies = analysis.find_companies(tweet)
logs.debug("Using companies: %s" % companies)
if companies:
trading.make_trades(companies)
twitter.tweet(companies, tweet)
if __name__ == "__main__":
logs = Logs(name="main", to_cloud=LOGS_TO_CLOUD)
# Restart in a loop if there are any errors so we stay up.
while True:
logs.info("Starting new session.")
twitter = Twitter(logs_to_cloud=LOGS_TO_CLOUD)
try:
twitter.start_streaming(twitter_callback)
except BaseException as exception:
logs.catch(exception)
finally:
twitter.stop_streaming()
logs.info("Ending session.")
示例2: __init__
# 需要导入模块: from logs import Logs [as 别名]
# 或者: from logs.Logs import catch [as 别名]
class Main:
"""A wrapper for the main application logic and retry loop."""
def __init__(self):
self.logs = Logs(name="main", to_cloud=LOGS_TO_CLOUD)
self.twitter = Twitter(logs_to_cloud=LOGS_TO_CLOUD)
def twitter_callback(self, tweet):
"""Analyzes Trump tweets, trades stocks, and tweets about it."""
# Initialize the Analysis, Logs, Trading, and Twitter instances inside
# the callback to create separate httplib2 instances per thread.
analysis = Analysis(logs_to_cloud=LOGS_TO_CLOUD)
logs = Logs(name="main-callback", to_cloud=LOGS_TO_CLOUD)
# Analyze the tweet.
companies = analysis.find_companies(tweet)
logs.info("Using companies: %s" % companies)
if not companies:
return
# Trade stocks.
trading = Trading(logs_to_cloud=LOGS_TO_CLOUD)
trading.make_trades(companies)
# Tweet about it.
twitter = Twitter(logs_to_cloud=LOGS_TO_CLOUD)
twitter.tweet(companies, tweet)
def run_session(self):
"""Runs a single streaming session. Logs and cleans up after
exceptions.
"""
self.logs.info("Starting new session.")
try:
self.twitter.start_streaming(self.twitter_callback)
except:
self.logs.catch()
finally:
self.twitter.stop_streaming()
self.logs.info("Ending session.")
def backoff(self, tries):
"""Sleeps an exponential number of seconds based on the number of
tries.
"""
delay = BACKOFF_STEP_S * pow(2, tries)
self.logs.warn("Waiting for %.1f seconds." % delay)
sleep(delay)
def run(self):
"""Runs the main retry loop with exponential backoff."""
tries = 0
while True:
# The session blocks until an error occurs.
self.run_session()
# Remember the first time a backoff sequence starts.
now = datetime.now()
if tries == 0:
self.logs.debug("Starting first backoff sequence.")
backoff_start = now
# Reset the backoff sequence if the last error was long ago.
if (now - backoff_start).total_seconds() > BACKOFF_RESET_S:
self.logs.debug("Starting new backoff sequence.")
tries = 0
backoff_start = now
# Give up after the maximum number of tries.
if tries >= MAX_TRIES:
self.logs.warn("Exceeded maximum retry count.")
break
# Wait according to the progression of the backoff sequence.
self.backoff(tries)
# Increment the number of tries for the next error.
tries += 1