2

There is a significant difference between my question and the given one. If I implement sleep as given on the link (which some people think my question is a duplicate of another one) then my whole app will hang for certain time. On the other hand I am looking for a scheduler so that my app will not hang but just after a certain period of time a .wav file will run. Meanwhile I would be able to do anything using my app. Hope that makes sense.

I am going to build an alarm clock. I think I can do this according to this Algorithm...

  • take current time using time.clock()
  • compare current time with the given time.
  • if current time is not given time then continue taking current time and compare whether current time is the given time or not.
  • if current time is the given time then play the .wav file.

The problem with this program is it will continuously run until the given time. So I am looking for better idea. Let's say if there is any Python module/class/function which can play a sound file on a given time. Is there any Python module/class/function which can wake up on a given time? Or my algorithm is used usually for all alarm clocks?

Trooper Z
  • 1,617
  • 14
  • 31
Cherry
  • 39
  • 7

2 Answers2

2

As far as I know there is not a good Python module to do this. What you are looking for though is a cron job. It allows you to schedule specific scripts to run at certain times. So your Python script would end up just being the code to play the .wav, and then you would need to create a cron job to tell your computer to execute that script at a certain time each day.

pzp
  • 6,249
  • 1
  • 26
  • 38
1

Have a look at the sched module.

Here's an example on how to use it:

import sched, time, datetime

def print_time():
    print("The time is now: {}".format(datetime.datetime.now()))

# Run 10 seconds from now
when = time.time() + 10

# Create the scheduler
s = sched.scheduler(time.time)
s.enterabs(when, 1, print_time)

# Run the scheduler
print_time()
print("Executing s.run()")
s.run()
print("s.run() exited")
The time is now: 2015-06-04 11:52:11.510234
Executing s.run()
The time is now: 2015-06-04 11:52:21.512534
s.run() exited
Raniz
  • 10,882
  • 1
  • 32
  • 64
  • can you provide me an easy example? – Cherry Jun 04 '15 at 02:34
  • will this halt my whole app for the given time or it will just schedule the code block to be executed after certain time. Meanwhile my whole app will be fully functional? Also is this a typo? Did you mean print time()? # Run the scheduler print_time() – Cherry Jun 04 '15 at 03:24
  • Also I didn't understand the program from line 10 to line 11. Can you please explain the lines? :) – Cherry Jun 04 '15 at 03:34
  • This will block your app. You can use multithreading or multiprocessing to make it not block your app. `print_time()` is correct, it's the function. Line 10 creates the scheduler and line 11 schedules `print_time()` to run at time `when`. Read the documentation I linked. – Raniz Jun 04 '15 at 03:42