Skip to content

Instantly share code, notes, and snippets.

@speendo
Created December 24, 2015 15:41
Show Gist options
  • Save speendo/91de6511443e75f18fed to your computer and use it in GitHub Desktop.
Save speendo/91de6511443e75f18fed to your computer and use it in GitHub Desktop.
Broken pipe problem with mpd
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from mpd import MPDClient
from mpd import ConnectionError as MPDConnectionError
from time import sleep
import logging
# Timeout
timeout_hours = 4
# Setup logging
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', filename="test_mpd.log", level="DEBUG", filemode="w")
logging.info("Log started")
# MPD Client
client = MPDClient()
client.timeout = 10
client.idletimeout = None
def client_connect():
logging.info("Attempt to (re-)connect to mpd server")
client.connect("localhost", 6600)
logging.info("Finished attempt to (re-)connect to mpd server")
logging.info(client.status)
client_connect()
# Add playlist entry
logging.info("Add playlist entry")
client.clear()
client.add("http://uwstream2.somafm.com:9016")
# Now start playing
logging.info("Start playing for the first time")
try:
client.play()
except Exception as e: # This is considered bad style - testing purposes
logging.exception("Lost MPD connection")
client_connect()
client.play()
sleep(10)
logging.info("This obviously works. Switching off now for %s hours.", timeout_hours)
try:
client.stop()
except Exception as e: # This is considered bad style - testing purposes
logging.exception("Lost MPD connection")
client_connect()
client.stop()
for i in range(0,20):
sleep(timeout_hours * 60 * 60)
logging.info("Trying to switch on again now")
try:
client.play()
except Exception as e: # This is considered bad style - testing purposes
logging.exception("Lost MPD connection")
client_connect()
client.play()
logging.info("Round %s completed", i)
sleep(10)
try:
client.stop()
except Exception as e: # This is considered bad style - testing purposes
logging.exception("Lost MPD connection")
client_connect()
client.stop()
logging.info("Finished")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment