如何安排任务在特定时间段的两个时间间隔之间每分钟运行?

我正在使用python 3.6&Schedule模块,需要安排任务在一周的某些特定日期在两个时间间隔之间每分钟运行。例如,假设任务计划为每分钟在每星期三和星期五的11:00 PM到01:30 AM的2.5小时时间间隔内进行安排。应该在两个给定的时间间隔之间每分钟运行一次,应该停止运行。

我尝试过的事情:

  1. 我尝试在#include <iostream> #include <string> #include <vector> #include <fstream> // Struct to hold each "line" of the data. struct Info { static const size_t DICE_LENGTH = 5; int dice_values[DICE_LENGTH]; std::string description; }; /** * @brief Creates the sample file (assuming it doesn't already exist) */ void createFile() { std::ofstream file ("file.txt"); if (file.is_open()) { file << "1 1 2 1 1 Aces\n"; file << "2 2 4 5 6 Twos\n"; file << "3 3 3 2 2 FullHouse\n"; file << "1 2 3 4 4 SmallStraight\n"; file << "2 3 4 5 6 LargeStraight\n"; file << "6 6 6 6 6 Sixes"; } else { throw "Error creating file"; } } int main() { createFile(); std::vector<Info> data; std::ifstream file("file.txt"); if (file.is_open()) { Info info; while (!file.eof()) { // read all the dice values (at most = DICE_LENGTH) for (size_t i = 0; i < Info::DICE_LENGTH; ++i) { file >> info.dice_values[i]; } // read the String std::getline(file,info.description); data.push_back(info); } } else { std::cerr << "Error opening the file for reading.\n"; return 1; } // display the data the was read from the file. for (const auto &d : data) { // Dice values. for (const auto &dice : d.dice_values) { std::cout << dice << " "; } // String value. std::cout << d.description << std::endl; } return 0; } 内使用schedule.every(1).minutes.do(job)

  2. 我已经建立了可以成功选择自己所选择的日子的逻辑,并且可以将其用于安排逻辑。

但是

  1. 那不会在指定时间停止,我无法了解如何引入结束时间限制。

  2. 我不知道如何将其安排在晚上11:00到01:30的时间,我不知道如何将01:30 AM安排在晚上11:00以后。

  3. 我对如何在自己选择的日子里安排时间一无所知。

有帮助吗?

yx_hcr 回答:如何安排任务在特定时间段的两个时间间隔之间每分钟运行?

我同意亨利·詹姆斯的回答。最好将cron用于此类任务。我认为cron默认安装在unix / linux上。

但是,如果您必须坚持使用python,则可以使用

import time
current_time=time.time()

这将为您提供当前的UNIX时间,您可以将其转换为时分秒格式。您将需要循环运行此程序以继续检查时间

,

如果使用Windows,则可以使用任务计划程序,或者如果UNIX / Linux可以使用Cron?

那样,您的脚本将运行然后结束,调度模块看起来就像它依赖脚本的连续运行。

本文链接:https://www.f2er.com/3167729.html

大家都在问