迭代并跟踪计数

时间:2019-04-24 16:01:10

标签: python iterator counter

我试图延迟浏览一些代码,并一直计算其运行时间。

我相信我必须使用while循环和sleep循环,但是我不确定它们的外观如何。

def download_image():
    while True:
        try:
            image = requests.get(newmeme).content
        except requests.RequestError:
            print("Retrying downloading")
            time.sleep(2 * 60)
        else:
            break
    with open('image.jpg', 'wb') as f:
        f.write(image)
    print("Download done.")

def upload_image(count):
    while True:
        try:
            twitter_API.update_with_media("image.jpg", status="dankmeme #{}".format(count))
        except tweepy.TweepError:
            print("Retrying uploading")
            time.sleep(2 * 60)
        else:
            break
    print("Image #{} uploaded at {:%H:%M}".format(count, datetime.now()))


count = 0

while True:
        download_image()
    upload_image()
        count = count+1
        time.sleep(300) 

0 个答案:

没有答案