是否可以让这个 .py 脚本每 20 分钟超时一次,然后自动再次自动 运行 ?

Is it possible make this .py script time out every 20 minutes and auto run again by it self?

是否可以让这个 .py 脚本每 20 分钟超时一次并自动再次 运行 ?

目前我正在使用 crontab 每 20 分钟重新运行它,但问题是它有时 运行 宁多个 .py 而不是真正地重新 运行 程序。我只希望它每 20 分钟重新运行,而不是每 20 分钟重新运行 另一个实例。

#!/usr/bin/env python

from TwitterFollowBot import TwitterBot
my_bot = TwitterBot("/home/TwitterFollowBot/config.txt")
my_bot.sync_follows()
my_bot.auto_unfollow_nonfollowers()
my_bot.auto_rt("@RtwtKing", count=2000)
my_bot.auto_rt("@ShoutGamers", count=2000)

您有多种方法可以做到这一点。 如果你只想用 Python 来做,你可以:

  • 使用线程,它会工作得很好,但这并不是线程设计的真正目的。
  • 使用守护进程(good example here
  • 做一个 Python 包装器,它将永远循环并在需要时调用您的脚本。它不太干净,但也不太矫枉过正。

包装解决方案示例:

目标是创建一个新的 python 脚本,它将处理计时器并在需要时执行您的 Twitter 代码。

1。更新您当前的代码以将其封装在一个方法中

假设您当前的文件名为 core.py

core.py:

from datetime import datetime
from TwitterFollowBot import TwitterBot

def get_feed():
    print "({}) LOG: get_feed starting".format(datetime.now())

    my_bot = TwitterBot("/home/TwitterFollowBot/config.txt")
    my_bot.sync_follows()
    my_bot.auto_unfollow_nonfollowers()
    my_bot.auto_rt("@RtwtKing", count=2000)
    my_bot.auto_rt("@ShoutGamers", count=2000)

这只是让你的代码成为一个函数,并添加一个日志行来打印函数执行时的当前时间。

2。制作一个处理计时器的包装器并调用你的推特代码

wrapper.py:

import time
from datetime import datetime

# Import your twitter code, so you can use it by calling 'get_feed()'
from core import get_feed

# Define constants here because it's easier to find it on top of file
# Delta between the last call and the next one, bascially time between two calls: 20 minutes
DELTA = 20 * 60
# Time your wrapper will take between each verification: 5 minutes
SLEEP_TIME = 5 * 60

# Initialize timer and call for the first time your method
# last_run will store timestamp of the last time you called get_feed()
last_run = time.time()
get_feed()

# Start an inifinite loop
while True:
    # Compute delta since last call
    # last_run_delta will store the time in seconds between last call and now
    last_run_delta = time.time() - last_run

    # If last_run_delta is upper than DELTA so the number of seconds you want to separate two calls is reached.
    if last_run_delta >= DELTA:
        # Because time is reached you want to run again your code and reset timer to can handle next call
        last_run = time.time()
        get_feed()

    # If you have not reach delta time yet, you want to sleep to avoid stack overflow and because you don't need to check each microseconds
    else:
        time.sleep(SLEEP_TIME)

使用 DELTA = 10SLEEP_TIME = 5 输出(core.py 每 10 秒调用一次,每 5 秒检查一次):

(2016-11-29 10:43:07.405750) LOG: get_feed starting
(2016-11-29 10:43:17.414629) LOG: get_feed starting
(2016-11-29 10:43:27.422033) LOG: get_feed starting
(2016-11-29 10:43:37.430698) LOG: get_feed starting
(2016-11-29 10:43:47.436595) LOG: get_feed starting

此方法唯一真正的好处是您不能同时启动同一个进程两次。因为它不是异步的,所以 get_feed 不能被调用两次,但是如果 get_feedSLEEP_TIMEDELTA 花费更多的时间,你会错过一些调用,所以不要 运行 每 20 分钟一次。

最后一件事,因为您要在 wrapper.py 中导入 core.py,所以您必须在与其他两个文件相同的文件夹中创建一个 __init__.py 文件。 (/path/to/project/ 应包含 __init__.py(空)、core.pywrapper.py)。

真正好的方法是创建一个守护进程,但它需要更多的技巧。