我希望机器人每天运行定义的功能。我做了一些研究,然后写出了这个:
def job():
print("task")
schedule.every().day.at("11:58").do(job)
while True:
schedule.run_pending()
time.sleep(1)
但是此代码阻止了所有其他功能,因此我从其他堆栈溢出答案中进行了更多研究,然后我能够编写出以下代码:
def job():
print('hi task')
def threaded(func):
job_thread = threading.Thread(target=func)
job_thread.start()
if __name__=="__main__":
schedule.every().day.at("12:06").do(threaded,job)
while True:
schedule.run_pending()
不幸的是,这也阻塞了我的整个代码。我需要一些不会阻塞代码其他部分的东西。该怎么做?
答案 0 :(得分:1)
因此,假设您想每天(24小时)将JSON文件上传到特定频道,您将执行以下操作
@tasks.loop(hours=24)
async def upload_json_to_discord():
channel_to_upload_to = client.get_channel(JSON_UPLOAD_CHANNEL_ID)
try:
await channel_to_upload_to.send(file=discord.File("id-list.json"))
print("JSON file upload success")
except Exception as e:
print(f"Upload JSON failed: {e}")
,然后您需要使用loop_func_name.start()
开始循环
所以在这种情况下:
upload_json_to_discord.start()
,因此这将使该功能每24小时运行一次
如果您在齿轮中,则必须在齿轮类的__init__
函数中使用它
您可以在文档中找到有关此信息的详细信息:https://discordpy.readthedocs.io/en/latest/ext/tasks/
答案 1 :(得分:1)
我参加聚会迟到了,但由于这个问题没有答案,我将把我的问题扔进去。
我假设你需要这个用于不和谐 py 机器人,因为它在你的标签中。正如@Zacky 所说,您应该使用 Tasks。
带有 @tasks.loop(hours=24)
装饰器的函数不会恰好每 24 小时启动一次,而是会在 24 小时内函数结束时安排。这意味着您可以将执行延迟到您想要的时间,然后让函数的流程一直持续到结束。一旦到达,该函数将等待 24 小时再次执行,它将对应于您想要的一天中的时间(加上函数的执行时间,所以如果您想要非常精确的函数执行,请小心)
我会这样做
def seconds_until(hours, minutes):
given_time = datetime.time(hours, minutes)
now = datetime.datetime.now()
future_exec = datetime.datetime.combine(now, given_time)
if (future_exec - now).days < 0: # If we are past the execution, it will take place tomorrow
future_exec = datetime.datetime.combine(now + datetime.timedelta(days=1), given_time) # days always >= 0
return (future_exec - now).total_seconds()
@tasks.loop(hours=24)
async def job(self):
await asyncio.sleep(seconds_until(11,58)) # Will stay here until your clock says 11:58
print("See you in 24 hours from exactly now")
正如@dan 指出的那样,如果等待之后的代码部分不是即时的,这可能会失败,因此这是一个更好的解决方案
def seconds_until(hours, minutes):
given_time = datetime.time(hours, minutes)
now = datetime.datetime.now()
future_exec = datetime.datetime.combine(now, given_time)
if (future_exec - now).days < 0: # If we are past the execution, it will take place tomorrow
future_exec = datetime.datetime.combine(now + datetime.timedelta(days=1), given_time) # days always >= 0
return (future_exec - now).total_seconds()
async def my_job_forever(self):
while True: # Or change to self.is_running or some variable to control the task
await asyncio.sleep(seconds_until(11,58)) # Will stay here until your clock says 11:58
print("See you in 24 hours from exactly now")
await asyncio.sleep(60) # Practical solution to ensure that the print isn't spammed as long as it is 11:58
答案 2 :(得分:0)
对于未来面临同样问题的人,使用 Tasks 的上述答案是正确的,但根据您使用的托管服务,可能会有更简单的解决方案。
一个流行的选择是 Heroku,您可以找到多个教程来设置您的机器人 like this one。
然后您可以使用 Heroku Scheduler 插件:
就是这样,如果你想在你的机器人中运行一个特定的函数,你可以在命令中指定参数并在你的代码中捕获它们,而不仅仅是像我这样的 python bot.py
。