1

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).

mysteri0us
  • 33
  • 1
  • 8
  • Does this answer your question? [Python - Start a Function at Given Time](https://stackoverflow.com/questions/11523918/python-start-a-function-at-given-time) – Thomas Weller Dec 11 '20 at 07:33
  • What you do there is a busy loop. It will consume a lot of CPU time. Even if you do that in the background, it will consume CPU time. If you then have multiple reminders, you'll reach 100% CPU – Thomas Weller Dec 11 '20 at 07:39
  • is there anyway to actually execute the block of code is a separate shell. that way the shell ends when the times finally reaches but the main program can still be running in the main shell(i use windows not linux, dont mistake the word "shell") – mysteri0us Dec 11 '20 at 07:57

1 Answers1

0

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!

GreenFish
  • 38
  • 1
  • 4