Python Forum

Full Version: Problem with playing sounds
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
New poster here. I've been having a problem that I don't know if it is related to Python or to Debian Linux. I have written a very simple script that plays a sound file. It doesn't seem to matter what sound player library I use. I've tried playsound, simpleaudio and pygame. They all have the same problem which is what makes me think it is Python. I run it in a cron job. The interval for the cron job doesn't seem to matter. What happens is that it runs for a while without a problem. Then the python script does not terminate, and is left in the process table. This happens every time the cron job runs, and it leaves an entry in the process table. Eventually the memory is eaten up and things start to go badly wrong.

Any ideas what could be happening here? I thought it might be that the sound file was left open, but I used the explicit calls in pygame to quit the mixer and unload the file. This did not help.

Also I can take out the functions that play the sound and there is no problem.

crontab entry
*/1 * * * * /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py

xmitLMStonetest1.py code
import time
from pydub.playback import play
from pydub import AudioSegment

time.sleep(3)

sound = AudioSegment.from_wav('/home/user/Downloads/LMS-TEST-ONv3.wav')
play(sound)

time.sleep(1)


process table residue
root@debian:~# ps -aux | grep python
root 571 0.0 0.2 118920 23108 ? Ssl 12:20 0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
root 1891 0.0 0.0 2480 576 ? Ss 13:35 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1892 0.4 0.9 742108 78968 ? SLl 13:35 0:08 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1908 0.0 0.0 2480 568 ? Ss 13:36 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1909 0.4 0.9 742108 77248 ? SLl 13:36 0:08 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1924 0.0 0.0 2480 508 ? Ss 13:37 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1925 0.3 0.9 742112 79276 ? SLl 13:37 0:07 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1940 0.0 0.0 2480 508 ? Ss 13:38 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1941 0.3 0.9 742112 79352 ? SLl 13:38 0:07 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1956 0.0 0.0 2480 508 ? Ss 13:39 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1957 0.3 0.9 742112 79328 ? SLl 13:39 0:07 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1972 0.0 0.0 2480 512 ? Ss 13:40 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1973 0.3 0.9 742112 79188 ? SLl 13:40 0:06 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1989 0.0 0.0 2480 508 ? Ss 13:41 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 1990 0.3 0.9 742116 77296 ? SLl 13:41 0:06 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2006 0.0 0.0 2480 576 ? Ss 13:42 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2007 0.3 0.9 742108 76872 ? SLl 13:42 0:06 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2022 0.0 0.0 2480 576 ? Ss 13:43 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2023 0.3 0.9 742108 79216 ? SLl 13:43 0:05 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2038 0.0 0.0 2480 508 ? Ss 13:44 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2039 0.3 0.9 742116 79432 ? SLl 13:44 0:05 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2054 0.0 0.0 2480 508 ? Ss 13:45 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2055 0.3 0.9 742112 77188 ? SLl 13:45 0:05 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2071 0.0 0.0 2480 572 ? Ss 13:46 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2072 0.3 0.9 742108 77064 ? SLl 13:46 0:05 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2088 0.0 0.0 2480 572 ? Ss 13:47 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2089 0.3 0.9 742116 79108 ? SLl 13:47 0:04 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2104 0.0 0.0 2480 568 ? Ss 13:48 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2105 0.3 0.9 742108 79160 ? SLl 13:48 0:04 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2121 0.0 0.0 2480 572 ? Ss 13:49 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2122 0.3 0.9 742116 77052 ? SLl 13:49 0:04 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2137 0.0 0.0 2480 572 ? Ss 13:50 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2138 0.3 0.9 742108 79292 ? SLl 13:50 0:04 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2154 0.0 0.0 2480 504 ? Ss 13:51 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2155 0.3 0.9 742116 77240 ? SLl 13:51 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2170 0.0 0.0 2480 504 ? Ss 13:52 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2171 0.3 0.9 742116 79252 ? SLl 13:52 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2186 0.0 0.0 2480 572 ? Ss 13:53 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2187 0.3 0.9 742112 79216 ? SLl 13:53 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2203 0.0 0.0 2480 512 ? Ss 13:54 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2204 0.3 0.9 742112 79312 ? SLl 13:54 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2219 0.0 0.0 2480 576 ? Ss 13:55 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2220 0.3 0.9 742108 78984 ? SLl 13:55 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2236 0.0 0.0 2480 512 ? Ss 13:56 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2237 0.3 0.9 742108 79116 ? SLl 13:56 0:03 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2253 0.0 0.0 2480 572 ? Ss 13:57 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2254 0.3 0.9 742116 77212 ? SLl 13:57 0:02 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2269 0.0 0.0 2480 504 ? Ss 13:58 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2270 0.3 0.9 742108 77172 ? SLl 13:58 0:02 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2285 0.0 0.0 2480 576 ? Ss 13:59 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2286 0.3 0.9 742112 79400 ? SLl 13:59 0:02 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2301 0.0 0.0 2480 512 ? Ss 14:00 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2302 0.3 0.9 742116 77128 ? SLl 14:00 0:02 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2318 0.0 0.0 2480 572 ? Ss 14:01 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2319 0.3 0.9 742116 77316 ? SLl 14:01 0:02 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2334 0.0 0.0 2480 512 ? Ss 14:02 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2335 0.3 0.9 742112 78908 ? SLl 14:02 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2350 0.0 0.0 2480 572 ? Ss 14:03 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2351 0.3 0.9 742108 77184 ? SLl 14:03 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2366 0.0 0.0 2480 512 ? Ss 14:04 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2367 0.4 0.9 742116 79180 ? SLl 14:04 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2382 0.0 0.0 2480 512 ? Ss 14:05 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2383 0.4 0.9 742108 75272 ? SLl 14:05 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2399 0.0 0.0 2480 504 ? Ss 14:06 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2400 0.4 0.9 742112 77148 ? SLl 14:06 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2415 0.0 0.0 2480 512 ? Ss 14:07 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2416 0.5 0.9 742112 75168 ? SLl 14:07 0:01 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2432 0.0 0.0 2480 508 ? Ss 14:08 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2433 0.6 0.9 742112 75284 ? SLl 14:08 0:00 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2448 0.0 0.0 2480 572 ? Ss 14:09 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2449 0.9 0.9 742116 77164 ? SLl 14:09 0:00 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2464 0.0 0.0 2480 576 ? Ss 14:10 0:00 /bin/sh -c /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2465 4.0 0.9 742108 75108 ? SLl 14:10 0:00 /usr/bin/python3 /home/user/Downloads/xmitLMStonetest1.py
root 2480 0.0 0.0 6372 708 ttyS0 S+ 14:10 0:00 grep python
why not simply in cronjob

aplay /path/to/file.wav
(May-24-2023, 08:35 PM)Axel_Erfurt Wrote: [ -> ]why not simply in cronjob

aplay /path/to/file.wav

That's a great idea. I will try it. I was not aware of aplay. Thank you!
(May-24-2023, 08:35 PM)Axel_Erfurt Wrote: [ -> ]why not simply in cronjob

aplay /path/to/file.wav

Hi Axel, FYI I tried out aplay in the cron job and this has a problem too. Rather than trying to diagnose this further I'm just not going to use cron and rather run a job in the background that has a timer in it. I've been trying this out and it seems to have no problem.
Did you test it in a terminal?
(May-25-2023, 06:56 PM)Axel_Erfurt Wrote: [ -> ]Did you test it in a terminal?

Yes I did. Works fine. It also works fine in the cron job for a while. Then something hangs and it stops working. It is the same effect as with all the other audio libraries I tried for Python using a Python script in the cron job. Same effect. Works for a while, then something hangs and the whole audio system locks up.

But in a terminal it works fine. Also if I loop the Python script to call the audio playback routine, then wait 1 min, then call it again, that works fine. Also works fine if you run it in the background. The problem only seems to appear when it is run as a cron job. Also, only audio functions seem to have this problem. I have a serial output to the USB serial port and this works fine also.