0

I have a Python2 script running in Arch Linux that is designed to loop forever - however, it stops running after so many hours.

Here is the script:

import RPi.GPIO as GPIO
import time
import urllib2
import xml.dom
from urllib2 import Request, urlopen, URLError, HTTPError
from xml.dom import minidom

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.output(15, True)
GPIO.output(16, True)

saved_store_pickup_order = False;

while True:
    req = Request('http://www.example.com/apiv2/pi/alerts/')
    try:
        response = urlopen(req)
    except HTTPError as e:
        print 'The server couldn\'t fulfill the request: ', e.code
    except URLError as e:
        print 'Failed to reach server: ', e.reason
    else:
        xml = response.read()
        xmldoc = minidom.parseString(xml)
        store_pickup = xmldoc.getElementsByTagName('current_store_pickup')
        if len(store_pickup):
            current_store_pickup_order = store_pickup[0].childNodes[0].nodeValue

            if not saved_store_pickup_order:
                saved_store_pickup_order = current_store_pickup_order

            if  current_store_pickup_order != saved_store_pickup_order:
                GPIO.output(15, False)
                GPIO.output(16, False)
                saved_store_pickup_order = current_store_pickup_order

    time.sleep(5)
    GPIO.output(16, True)
    time.sleep(25)

I'm wondering if there is a timeout period by default with executing Python scripts.

Also, this script is started at boot by systemd if that makes a difference.

Emery King
  • 3,550
  • 23
  • 34
  • No, python does not timeout. Yes, started by `systemd` could affect depending on how you start it. – Antti Haapala -- Слава Україні May 28 '14 at 18:25
  • 1
    Can you tell if it's failing due to an error? It's possible, for instance, that a glitch in your internet connection is causing the request to fail, which would crash the script. – BrenBarn May 28 '14 at 18:26
  • I've run it manually and if it gets a 404 for example, it continues running fine. – Emery King May 28 '14 at 18:26
  • If the case is true where a connection failure crashes it; How do i prevent that? – Emery King May 28 '14 at 18:28
  • Simplest way would be to remove two exception handlers, remove else keyword, and move except block just before time.sleep(5). You need to catch Exception. That should be enough for your script. Your script CAN fail when for example it tries to parse bad xml. – tvrtko May 28 '14 at 18:41

1 Answers1

1
  1. put the entire loop in a try: except: block

  2. consider moving from print statements to using the logging library.

Example (untested):

import logging
mylog = logging.getLogger(__name__)
mylog.info('start')
while True:
    try:
       out = dostuff(data)
       mylog.debug('got {}, output={}'.format(data, out))
    except KeyboardInterrupt:
       break
    except Exception:
       mylog.exception('uhoh')
mylog.info('done')
Blckknght
  • 100,903
  • 11
  • 120
  • 169
johntellsall
  • 14,394
  • 4
  • 46
  • 40