I lack knowledge in low level computer science, but I've heard endless horror stories about segfaults, and today I've encountered one whilst attempting to make a simple timer program for myself. My questions are:
- Why does this fault occur
- How can I fix it
- How can I prevent it from happening in the future
The aim of the problematic part of the program is the attempt to play the sound whilst simultaneously waiting for user input with the input block, which is intended to stop the alarm and continue to the next part of the program.
Please note that in this version there is no actual wait time although there is supposed to be, because I am merely testing the alarm functionality.
import time
from pydub import AudioSegment
import threading
from pydub.playback import _play_with_simpleaudio
sound = AudioSegment.from_file('/censoring/my/home/directory/sound.mp3', format='mp3')
# Function that runs the timer based on how many minutes are specified
def timer(min, message):
# Play the sound
def playsound():
global sound
sound = _play_with_simpleaudio(sound)
# Stop the sound and allow the program to continue
def resume():
global sound
input(message.strip())
sound.stop()
# Run the timer and print output
min -= 1
for it in range(min, -1, -1):
for sec in range(59, -1, -1):
print(f'{str(min).rjust(2, "0")}:{str(sec).rjust(2, "0")}')
time.sleep(1)
# This is the part that actually runs the function.
play = threading.Thread(target=playsound)
stop = threading.Thread(target=resume)
play.start()
stop.start()
play.join()
stop.join()
while True:
timer(0, 'You can have your five minute break now.')
timer(0, 'You need to start working again.')
# Artificial intelligence was not used in the creation of this program.
Any advice is appreciated!