1

Python 3.8
OS windows 10

I'm trying to create an alarm with a GUI. Basically, when a condition is met (current time == set time) a widget must pop up with two buttons: Cancel, and Run this other code. if I press any of the buttons the alarm should stop making any sound because both acknowledge that the alarm went off.

I'm displaying the widget and using multiprocessing to play the alarm music, but I can't make the alarm stop. This is a test code that simply plays the alarm and asks the user to press enter to stop it. If I can make this work I will be able to finish my widget alarm

import multiprocessing
from playsound import playsound

def Child_process():
   print("Playing music")
   playsound('Perfect_Ring_Tone.mp3', block=False)

def terminator(process):
    process.terminate()
    
if __name__ == '__main__':
    # Create widget......

    # Play alarm sound
    P = multiprocessing.Process(target = Child_process)
    P.start()
    P.join()
    input("Press Enter to acknowledge the alarm")
    terminator(P)
    print("Child Process successfully terminated")
    # Do some more code...

If I do this the alarm never make a sound unless I remove the block but then I'd have to wait for it to finish which defeats the purpose

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61

1 Answers1

0

The problem is that you've used join() function which waits for the child process to die. This means that the main program stops until the child process have been terminated and the input instruction will never been executed because the function would never stop.

So simply remove P.join()

zbx0310
  • 64
  • 8
  • Hi Darcy, I thought about that and it makes sense. But, if I remove it then the child process never executes for whatever reason. Besides by adding ```block=False``` playsound plays in the background and allows the main code to keep running so ```join()``` would make sence – Jeyc Yelamo Dec 22 '20 at 19:01
  • Didn't you test it ? There's no need to use ```join()``` – zbx0310 Dec 22 '20 at 19:09
  • I did!, I commented out the ```P.join()``` and it doesn't play any sound and it doesn't print "Playing music" – Jeyc Yelamo Dec 22 '20 at 19:14
  • Ah I've removed ```P.join()``` then ran it and it worked. – zbx0310 Dec 22 '20 at 19:30
  • How is that possible? I tried running it without the ```join()``` and it jumps straight to "Press Enter to acknowledge the alarm" without starting the child process – Jeyc Yelamo Dec 22 '20 at 19:33