'import soundcard' throws an error when run through a systemd service
See original GitHub issueHey, I have the following code:
#test.py
import soundcard
while True:
pass
It runs fine with
python test.py
But it does not when I want to run it using a systemd service.
Here’s the service definition:
[Unit]
Description=Soundcard test service
Requires=sound.target
After=sound.target
[Service]
ExecStart=python test.py
User=pi
[Install]
WantedBy=default.target
And the error it throws:
pi@raspberrypi:sudo systemctl start test.service
pi@raspberrypi:~ $ sudo systemctl status test.service
● caravane.service - Soundcard test service
Loaded: loaded (/lib/systemd/system/test.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2021-07-26 20:58:23 CEST; 3s ago
Process: 2649 ExecStart=/usr/bin/python /home/pi/test.py (code=exited, status=1/FAILURE)
Main PID: 2649 (code=exited, status=1/FAILURE)
juil. 26 20:58:23 raspberrypi python[2649]: import soundcard
juil. 26 20:58:23 raspberrypi python[2649]: File "/home/pi/.local/lib/python3.7/site-packages/soundcard/__init__.py", line 4, in <module>
juil. 26 20:58:23 raspberrypi python[2649]: from soundcard.pulseaudio import *
juil. 26 20:58:23 raspberrypi python[2649]: File "/home/pi/.local/lib/python3.7/site-packages/soundcard/pulseaudio.py", line 261, in <module>
juil. 26 20:58:23 raspberrypi python[2649]: _pulse = _PulseAudio()
juil. 26 20:58:23 raspberrypi python[2649]: File "/home/pi/.local/lib/python3.7/site-packages/soundcard/pulseaudio.py", line 72, in __init__
juil. 26 20:58:23 raspberrypi python[2649]: assert self._pa_context_get_state(self.context)==_pa.PA_CONTEXT_READY
juil. 26 20:58:23 raspberrypi python[2649]: AssertionError
juil. 26 20:58:23 raspberrypi systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
juil. 26 20:58:23 raspberrypi systemd[1]: test.service: Failed with result 'exit-code'.
I tried with the sounddevice module instead of soundcard and I dont get this error so I guess the problem comes from soundcard and not PulseAudio?
Why all this: I’m making a sound installation, the main python program runs fine on the Raspberry (using multiple sound cards handled by the soundcard module, threading, …), but I struggle to make it start at boot. It seems to run too early with rc.local or a crontab (because sound cards and/or pulseaudio itself are not initialized yet, I guess), so I tried with systemd but I faced the problem described above.
I’m still learning programming and how linux work so sorry if I missed something obvious and if the problem is actually unrelated to soundcard itself.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (3 by maintainers)
@ronny-rentner @Teufeuleu : I have the same issue. It works fine on systemd service in ubuntu 18.04 with pulseaudio version 11.1 It fails on systemd service in ubuntu 20.04 pulseaudio version 13.9. soundcard library version is 0.4.1 on both machines. paplay seems to be fine though - both via systemd service as well as command line execution.
Update: After adding ‘sound.target’ as a dependency and sourcing these 2 env variables, it started working in systemd.
@Teufeuleu: You could try to use pulseaudio’s
paplay
from your service to see if it actually has access to pulseaudio and can play something. Then you know if the issue is with your pulseaudio setup or with SoundCard.