Running Python-socketio With SystemD To Play Sounds At Background
My SystemD service file looks like this:
[Unit] Description=XXX After=sound.target network.target Wants=sound.target [Service] ExecStart=/usr/bin/python3 -u raspberry.py WorkingDirectory=/home/pi/Desktop Restart=always User=pi PrivateTmp=true [Install] Alias=XXX WantedBy=multi-user.target
The python script is a classic python-socketio client which should listen for events like "listen" and "play". The main part of code looks like this:
import subprocess import socketio HOST = "https://XXX.ngrok.io" sio = socketio.Client(engineio_logger=True) ... @sio.on('play') def play(data): print("play") subprocess.call(["espeak", "'Not working'"]) if __name__ == '__main__': subprocess.call(["espeak","'Initialized'"]) sio.connect(HOST) sio.wait()
When I set up the service to run at booting, the first calling of espeak is executed and socket connection with my server is established but then if I send an event (through my server) the second calling of espeak is not working (there is no sound). If I look into logs through
journalctl -u XXX I will see that the function is called because the print statement is executed.
What comes to my mind is that it is because of running the subprocess call from a different thread, but I am not sure.. any ideas?
The solution is related to my other question at Raspberry Pi forum. The main problem was the inability of root to play sounds. When I was debugging this problem, I found that because of
User=pi the service is started as user pi. But when I call
@sio.on('play') part it was under called as user root. It was happening only in
@sio.on('play') part. If I did it in
if __name__ == '__main__': part, the calling was under
pi user. Still don't know why it was happening but solution was not using AIY hat version of Raspbian but classic version Raspbian Stretch Lite.
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module