How to run a loop from a program in background while the code proceeds to do other things

867 views Asked by At

so, i was trying to make a personal assistant and i wanted to make a reminder type of commnd. here is the code of the reminder(notification). and the speak and takecommand fuctions are custom.

elif "remind" in query:
        speak("what should i remind you")
        messageT = takeCommand()
        speak("ok, so when should i remind you.")
        timeOfReminder = takeCommand()
        while True:
            current_time = tm.strftime("%H:%M:%S")
            if current_time == timeOfReminder:
                print(current_time)
                break;

        toaster = ToastNotifier()
        toaster.show_toast(messageT,
                        duration=10)

so the problem is not with this code, but the code contains a while loop which prohibits me to proceed with my code(this whole code is also a part of a while loop which contains all the command). i wanna know how to like run this loop in parellel of the other main while loop(this while loop checks the time while the main while loop checks what command the user is giving.) is there any way to do this (if you want i can post my full code if it helps).

1

There are 1 answers

0
GreenFish On BEST ANSWER

Use the threading library in Python 3.

import threading

Create a function:

def my_long_function():
    while 1:
        print("runs in background")
        time.sleep(1)

long_thread = threading.Thread(target=my_long_function)
long_thread.start()

while 1:
    print("Running in foreground")
    time.sleep(3)

Hope this works for you!