r/learnpython • u/Sanduhstorm • Sep 14 '26
Segmentation fault when attempting to use the 'threading' module to run tasks concurrently
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!
View all comments
Show parent comment
1
u/fllthdcrb Sep 14 '26 edited Sep 15 '26
Yes, asyncio, as you might guess from the last part of the name, is meant for handling I/O tasks, where most of the time is spent waiting for input.
Threads use preemptive multitasking (i.e. the OS controls their execution), and can take advantage of multiple CPU cores. async, OTOH, is normally single-threaded and works by cooperative multitasking, where each task runs until it voluntarily gives up control to the scheduler (which is not part of and doesn't involve the OS, but runs in the application itself); an advantage over threads is you have to worry much less about race conditions, since a task will never be interrupted at some arbitrary point to have another task operate on the same data, unless threads are deliberately employed.
But then, Python has mostly had this problem of the Global Interpreter Lock (GIL), where only one thread is allowed to run Python code at any given moment, due to lack of thread safety. There has been effort in the last couple of years to get rid of the GIL with so-called "free-threading" interpreters, but libraries have to cooperate to make it work, and some things in Python code might have to be changed to adapt.