我有一个类,可以在while循环中启动一个线程。我试图安排线程类在特定的时间内启动,但它不起作用:
def test():
if __name__ == "__main__":
main()
schedule.every().day.at("17:25:50").do(test)
即使时间达到"17:25:50“,该函数也不会执行任何操作。
我的完整代码:
import discord
import random
import time
import asyncio
import schedule
from facebook_scraper import get_posts, _scraper, exceptions
from discord.ext import commands, tasks
import threading
import time
import re
class LEDManager(threading.Thread):
def __init__(self, id_manager):
threading.Thread.__init__(self)
self.id_manager = int(id_manager)
def run(self):
while True:
try:
wanted = "Pecahan setiap negeri (Kumulatif):" # wanted post
for post in get_posts("myhealthkkm", pages=5):
if post.get("post_text") is not None and wanted in post.get("post_text"):
# print("Found", t)
listposts.append(post.get("post_text"))
# append until 3 page finish then go here
time.sleep(1)
print(listposts)
global listView
if listposts != 0:
listView = listposts.copy()
print(listView)
listposts.clear()
except exceptions.TemporarilyBanned:
print("Temporarily banned, sleeping for 10m")
time.sleep(600)
def main():
thread_id = ("0")
led_index = 0
thread_list = list()
for objs in thread_id:
thread = LEDManager(led_index)
thread_list.append(thread)
led_index += 1
for thread in thread_list:
thread.start()
time.sleep(1)
def test():
if __name__ == "__main__":
main()
schedule.every().day.at("17:25:50").do(test)
发布于 2021-08-11 03:36:32
您忘记添加以下行:
while True:
schedule.run_pending()
time.sleep(1)
您应该将它们添加到文件的末尾,这样系统将永远检查“作业”是否需要完成(如果时间是"17:25:50")。
下面是查看如何使用schedule模块的完整文档:https://schedule.readthedocs.io/en/stable/
https://stackoverflow.com/questions/68741146
复制相似问题