简体   繁体   English

我如何在某个时间戳记时间运行python函数。 没有外部软件?

[英]How do i run a python function at a certain timestamp time. Without outside software?

I want to tell a python script to run a certain function when a certain timestamp time happens 我想告诉python脚本在某个时间戳记时间发生时运行某个函数

I have already looked for running time specific functions but i could not find anything that answered this specific question about counting time stamps 我已经在寻找运行时间特定的功能,但找不到任何能回答有关计数时间戳的特定问题的东西

#input is number of days till due date
dueDate = int(input('Days until it is due: '))

#86400 seconds in a day
days = dueDate * 86400

#gets current time stamp time 
currentT = int(time.time())

#gets the timestamp for due date 
alarm = days+currentT

the aim is to find the python function which can run another function from within the script at when a specified future time stamp occurs 目的是找到在指定的未来时间戳出现时可以从脚本内运行另一个函数的python函数

Build into python is the sched module. sched模块sched在python中。 Here is a pretty good write-up on it, and here is the official documentation. 是一篇很好的文章, 是官方文档。 With scheduler.enter you can schedule with a delay, and with scheduler.enterabs you can schedule for a specific time. 使用scheduler.enter可以延迟计划,使用scheduler.enterabs可以计划特定时间。

import sched
import time

scheduler = sched.scheduler(time.time, time.sleep)

def print_event(name):
    print('EVENT:', time.time(), name)

now = time.time()
print('START:', now)

scheduler.enterabs(now+2, 2, print_event, ('first',))
scheduler.enterabs(now+5, 1, print_event, ('second',))

scheduler.run()

Outputs: 输出:

START: 1287924871.34
EVENT: 1287924873.34 first
EVENT: 1287924874.34 second

You could just sleep the script for that long. 您可以将脚本休眠那么长时间。

time.sleep(alarm)

Source: python docs 资料来源: python docs

Schedule is a good python module for doing this. Schedule是一个很好的python模块。

Usage: (From Documentation) 用法:(来自文档)

Install 安装

$ pip install schedule

Usage 用法

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中的特定时间运行某个函数? - How can I run a certain function for a specific time in Python? 如何在我的Swift软件应用程序中为我的函数计算运行Python代码? - How do I run Python code for my function calculations in my Swift software App? Python:如何将日期和时间(在 1 个字符串中)转换为时间戳 - Python: How do i convert date and time (in 1 string) into a timestamp Python-什么是运行基于时间的方法/函数的最佳方法。 (24小时) - Python - Whats the best way to run a method/function based on Time. (24h) Python-telegram-bot api 'update.message.date' 返回错误的时间。 我该如何解决? - Python-telegram-bot api 'update.message.date' is returning the wrong time. how do i fix this? 如何在一段时间后自动注销用户。 Python Tkinter - How to auto logout user after certain period of time. Python Tkinter 如何通过 python 调用/运行软件? - How can I call/run a software by python? 如何在没有操作系统的情况下运行 python - How do I run python without an OS 如何在Python中为同时函数调用打上时间戳? - How do I timestamp simultaneous function calls in Python? 你如何在 Python 中的某个时间启动一个函数? - How do you start a function at a certain time in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM