有没有可能让这个.py脚本每20分钟超时一次,然后自动运行?

2024-04-25 22:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有可能让这个.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)

Tags: 实例py程序env脚本autobinmy
1条回答
网友
1楼 · 发布于 2024-04-25 22:34:08

你有几种方法可以做到这一点。 如果您只想使用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。制作一个包装器来处理计时器并调用你的twitter代码

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(核心.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)。你知道吗

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

相关问题 更多 >