[Python] Python time-interval check returns None when two lecture intervals share the same...

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I’m building an attendance system in Python where lecture timings come from a MySQL table:

    day | start | end
    ----+------------+-----------
    Mon | 10:00:00 | 10:45:00
    Mon | 10:45:00 | 11:30:00
    ...


    My goal is:


    1. Fetch today’s lecture timings from the database


    2. Continuously check the current time


    3. Start face-detection automatically only during the active lecture


    4. Keep waiting (not exit) when no lecture is active yet

    I am currently doing this

    conn = mysql.connector.connect(...)
    cursor = conn.cursor()
    cursor.execute("SELECT start, end FROM timetable WHERE day=%s", ("Mon",))
    rows = cursor.fetchall()

    lectures = []
    for s, e in rows:
    start = datetime.combine(date.today(), s)
    end = datetime.combine(date.today(), e)
    lectures.append((start, end))

    def get_current_lecture():
    now = datetime.now()
    for start, end in lectures:
    if start <= now < end:
    return (start, end)
    return None

    while True:
    current = get_current_lecture()
    if current is None:
    break # <-- problem
    else:
    run_face_detection_until(current[1])


    The problem:
    If I start the script before the first lecture starts, get_current_lecture() returns None and the loop exits immediately.
    But I need the script to wait and keep checking until the lecture actually starts, instead of stopping.

    Question:
    What is the correct way to integrate database-based time intervals with a Python loop so that:


    • the script runs continuously


    • returns None when idle


    • but does NOT exit the program


    • and automatically starts processing when the lecture time arrives?

    Should I redesign the loop, or is there a better pattern for time-based scheduling with database intervals?

    Continue reading...

Compartilhe esta Página