擅长:python、mysql、java
<p>您可以在一个循环中抛出try/except语句,该循环有一个睡眠计时器,可以在两次尝试之间等待多长时间。像这样:</p>
<pre><code>import time
# Upload to Xivity
api = xively.XivelyAPIClient("[MY_API_KEY")
feed = api.feeds.get([MY_DATASTREAM_ID])
now = datetime.datetime.utcnow()
feed.datastreams = [xively.Datastream(id='temps', current_value=tempF, at=now)]
### Try loop
feed_updated = False
while feed_updated == False:
try:
feed.update()
feed_updated=True
except: time.sleep(60)
</code></pre>
<p><em>编辑</em>正如Dano指出的,最好有一个更具体的except语句。在</p>
^{pr2}$
<p><em>编辑</em>一个通用的except语句。在</p>
<pre><code>### Try loop
feed_updated = False
feed_update_count = 0
while feed_updated == False:
try:
feed.update()
feed_updated=True
except:
time.sleep(60)
feed_update_count +=1 ## Updates counter
if feed_update_count >= 60: ## This will exit the loop if it tries too many times
feed.update() ## By running the feed.update() once more,
## it should print whatever error it is hitting, and crash
</code></pre>