python time sleep 无效_time.sleep()暂停执行-python

所以我在python中制作了一个reddit机器人,使用下面的代码

repository

是的。

而且工作正常。但根据reddit的指导方针,我只能每10分钟发表一次评论。所以我用的是

for

在这里循环,代码的最后一部分如下:

i=1

for submission in subreddit.hot(limit=10):

if not os.path.isfile("posts_replied_to.txt"):

posts_replied_to = []

# Or load the list of posts we have replied to

else:

with open("posts_replied_to.txt", "r") as f:

posts_replied_to = f.read()

posts_replied_to = posts_replied_to.split("\n")

posts_replied_to = list(filter(None, posts_replied_to))

# Pull the hottest 10 entries from a subreddit of your choosing

#print(submission.title)

if(i%5!=0):

# Make sure you didn't already reply to this post

if submission.id not in posts_replied_to:

# Not case sensitive

if re.search("backpacking", submission.title, re.IGNORECASE):

# Reply

foo=['Awesome','Pretty nice eh!','Cool','Very beautiful','I would love to go there',\

'I also want to try it out now', 'Really cool', 'Can\'t wait to go there', 'Nice']

random_index = randrange(len(foo))

submission.reply(foo[random_index])

print("Bot replying to : ", submission.title)

# Store id in list

posts_replied_to.append(submission.id)

else:

if submission.id not in posts_replied_to:

# Not case sensitive

if re.search("backpacking", submission.title, re.IGNORECASE):

# Reply

foo=['https://****.in']

random_index = randrange(len(foo))

submission.reply(foo[random_index])

print("Bot replying to : ", submission.title)

# Store id in list

posts_replied_to.append(submission.id)

i+=1

with open("posts_replied_to.txt", "w") as f: # Write updated list to file

for post_id in posts_replied_to:

f.write(post_id + "\n")

time.sleep(600) #10 minutes delay

因此,对于背包下排名前十的subreddits,我想每10分钟发布一次,每5条评论,我想发布一个链接,这就是为什么我使用

if (i%5)!=0

是的。

现在你可以看到我添加了

time.sleep()

最后。当我把它注释掉时,它执行得很好,在第二次注释时,它显示API异常错误,尝试在9分钟后发布,这是基于Reddit准则的合法性。

但是当我使用

时间。睡眠()

函数它不再执行了,我已经等了10多分钟,大约半个小时,但它仍然没有执行,我不知道为什么!

每天的评论数量是否也有限制?我没有找到,所以如果有人知道请!

我的主要动机是每10分钟执行一次发布功能,而不是

时间。睡眠()

如果有其他合适的功能。请不要推荐cronjob。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值