Skip to content

Instantly share code, notes, and snippets.

@speendo
Created December 15, 2015 19:18
Show Gist options
  • Save speendo/0d9c1a028d045de3f7a6 to your computer and use it in GitHub Desktop.
Save speendo/0d9c1a028d045de3f7a6 to your computer and use it in GitHub Desktop.
BrokenPipeError when idle for a couple of hours
#!/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 MPDConnectionError:
logging.info("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 MPDConnectionError:
logging.info("Lost MPD connection")
client_connect()
client.stop()
sleep(timeout_hours * 60 * 60)
logging.info("Trying to switch on again now")
try:
client.play()
except MPDConnectionError:
logging.info("Lost MPD connection")
client_connect()
client.play()
logging.info("If you read this, also the second play command was executed")
sleep(10)
try:
client.stop()
except MPDConnectionError:
logging.info("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