[英]Why does my twitter bot post more than expected?
I am using the code time.sleep(3600)
and it is tweeting more than every 3600 seconds.我正在使用代码
time.sleep(3600)
并且它每 3600 秒就会发推一次。 Why is this happening?为什么会这样?
Currently it is tweeting at 9 minutes past, then 32 minutes past.目前它在 9 分钟后发推文,然后是 32 分钟后。
Edit:编辑:
Here is the code.这是代码。 The only other reason this could be happening is that this may be running in multiple instances accidentally.
这可能发生的唯一另一个原因是这可能会意外地在多个实例中运行。 I will check that.
我会检查的。
# tweepy will allow us to communicate with Twitter, time will allow us to set how often we tweet
import tweepy, time
#enter the corresponding information from your Twitter application management:
CONSUMER_KEY = 'mykey' #keep the quotes, replace this with your consumer key
CONSUMER_SECRET = 'mykey' #keep the quotes, replace this with your consumer secret key
ACCESS_TOKEN = 'my-my' #keep the quotes, replace this with your access token
ACCESS_SECRET = 'mykey' #keep the quotes, replace this with your access token secret
# configure our access information for reaching Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# access Twitter!
api = tweepy.API(auth)
# open our content file and read each line
filename=open('content.txt')
f=filename.readlines()
filename.close()
# for each line in our contents file, lets tweet that line out except when we hit a error
for line in f:
try:
api.update_status(line)
print("Tweeting!")
except tweepy.TweepError as err:
print(err)
time.sleep(3600) #Tweet every hour
print("All done tweeting!")
This may be caused by your module not being protected from running when imported.这可能是由于您的模块在导入时未受到保护而无法运行。 That means every time your module is imported, (could happen on
这意味着每次导入模块时,(可能发生在
from package import *
), your code is interpreted and a new loop is created. ),您的代码将被解释并创建一个新循环。
You could ensure your code is run only when you want it to run with this :您可以确保您的代码仅在您希望它运行时运行:
Make a function from your code, let's name it main().从您的代码中创建一个函数,我们将其命名为 main()。
You can then check if your module is called as a script.然后,您可以检查您的模块是否作为脚本调用。
def main():
# tweepy will allow us to communicate with Twitter, time will allow us to set how often we tweet
import tweepy, time
#enter the corresponding information from your Twitter application management:
CONSUMER_KEY = 'mykey' #keep the quotes, replace this with your consumer key
CONSUMER_SECRET = 'mykey' #keep the quotes, replace this with your consumer secret key
ACCESS_TOKEN = 'my-my' #keep the quotes, replace this with your access token
ACCESS_SECRET = 'mykey' #keep the quotes, replace this with your access token secret
# configure our access information for reaching Twitter
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
# access Twitter!
api = tweepy.API(auth)
# open our content file and read each line
filename=open('content.txt')
f=filename.readlines()
filename.close()
# for each line in our contents file, lets tweet that line out except when we hit a error
for line in f:
try:
api.update_status(line)
print("Tweeting!")
except tweepy.TweepError as err:
print(err)
time.sleep(3600) #Tweet every hour
print("All done tweeting!")
if __name__ == "__main__":
main()
If you have to use your code from another script, you can use如果您必须使用另一个脚本中的代码,则可以使用
from your_module import main
main()
Or from a command line :或者从命令行:
python -m your_module
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.