Python 在正在运行的线程中运行cron调度程序

Python 在正在运行的线程中运行cron调度程序,python,cron,telegram-bot,apscheduler,telepot,Python,Cron,Telegram Bot,Apscheduler,Telepot,我正在写一个电报机器人,我想安排它每天在特定的时间,以cron方式发送一条自动消息。我正在使用apscheduler来完成它,但是我无法让cron函数工作。时间间隔安排很好,但这不是我想要的 我不想在外部执行.py文件,因为我需要电报机器人来检测用户的/start消息 所以我想做的是检测用户的/start消息,然后启动调度器。此后,机器人将每天晚上8点向用户发送消息 Cron调度没有启动,我不知道为什么。我怀疑这是因为它不能在我正在运行的主线程中运行?任何建议都会有帮助!多谢各位 import

我正在写一个电报机器人,我想安排它每天在特定的时间,以cron方式发送一条自动消息。我正在使用apscheduler来完成它,但是我无法让cron函数工作。时间间隔安排很好,但这不是我想要的

我不想在外部执行.py文件,因为我需要电报机器人来检测用户的/start消息

所以我想做的是检测用户的/start消息,然后启动调度器。此后,机器人将每天晚上8点向用户发送消息

Cron调度没有启动,我不知道为什么。我怀疑这是因为它不能在我正在运行的主线程中运行?任何建议都会有帮助!多谢各位

import time
import telepot
import json
from telepot.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup # for custom keyboard
from telepot.delegate import pave_event_space, per_chat_id, create_open
from apscheduler.schedulers.blocking import BlockingScheduler

## Load token
TOKEN = 'TOKEN NUMBER'

# The main body
class main(telepot.helper.ChatHandler): # Implement continuous dialogue with user using DelegatorBot

    global counter1
    counter1 = 0

    global counter2
    counter2 = 0

    def __init__(self, *args, **kwargs):
        super(main, self).__init__(*args, **kwargs)

        # Initialize and create empty dictionary for storing data.
        self.indicator = '0'
        self.data = {} # initialize an empty dictionary for storing data.



    def on_chat_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance(msg) # this is very important.
        # Debugging
        print(content_type, chat_type, chat_id)
        print(msg['text'])
        global counter1
        global counter2
        scheduler = BackgroundScheduler()
        #scheduler_cron = BlockingScheduler()

        # Survey function
        def survey():...
   
            return

        def repeat_message():
            bot.sendMessage(chat_id, text='type /survey to repeat survey.')
            print("Reminder sent.")

        scheduler_cron.add_job(repeat_message, 'cron', day='*', week='*', day_of_week='*', hour=20, minute=00)

     

        # Detect start messages.
        while True:
            if counter2 == 11: # If survey ends AKA 11 Qns done. Restart the counters.
                counter1 = 0
                counter2 = 0

            # once bot starts, ask user to repeat survey at a specific time of the day.

            if counter1 == 0: # If survey ends or at the very beginning of using the bot.
                # Start message.
                if msg['text'] == '/start':  # /starts initiates bot. User gets greeted with a welcome message describing what the bot will do.
                    bot.sendMessage(chat_id,
                                    text='Hello there.',
                                    parse_mode='Markdown')


                    
                    scheduler_cron.start()
                    print("Scheduler started.")

                # Start survey after initializing the bot
                elif msg['text'] == '/survey':  # /survey initiates survey.
          
                    print("Survey started...")
                    #counter1 = 0
                    counter1 += 1 

                else:
                    bot.sendMessage(chat_id, text='I do not recognise your message.')
                    msg['text'] = '' # resets msg.

            
            # User starts survey
            if counter1 == 1: # if user types /survey, counter1 +=1 and when counter1 == 1, run survey function.
                survey() # starts survey
                counter2 += 1 

      
            break



bot = telepot.DelegatorBot(TOKEN, [pave_event_space()(per_chat_id(), create_open, main, timeout=60),]) 

MessageLoop(bot).run_as_thread() # Initiates the bot on telegram. Listens for user's response. If this is stopped, the entire bot stops.
print('Listening...')
while True:
    time.sleep(1)
编辑:我发现,如果我有另一个线程在后台运行,则apscheduler的cron无法工作,如他们文档中所述:

BlockingScheduler: use when the scheduler is the only thing running in your process
BackgroundScheduler: use when you’re not using any of the frameworks below, and want the scheduler to run in the background inside your application

所以这意味着我不能使用apscheduler使我的机器人工作。有谁知道任何类似cron的替代方案,它允许我安排电报机器人在一天中的特定时间向用户发送消息?最好是与电报API配合使用。

您是否按照?@AlexGrönholm hello中的说明调试过调度程序。我不知道有调试功能。无论如何,我调试了它,发现调度程序按预期工作。但是,一旦启动调度程序,我的代码就不会转到下一行。所以我想知道如何在后台运行cron调度程序,然后继续下一行代码。间隔调度可以像这样工作,但是我想在一天中的特定时间安排代码。谢谢你试过按照?@AlexGrönholm hello中的说明调试调度程序了吗。我不知道有调试功能。无论如何,我调试了它,发现调度程序按预期工作。但是,一旦启动调度程序,我的代码就不会转到下一行。所以我想知道如何在后台运行cron调度程序,然后继续下一行代码。间隔调度可以像这样工作,但是我想在一天中的特定时间安排代码。谢谢