How to protect program hang

This is the place for queries that don't fit in any of the other categories.

How to protect program hang

Postby AM181179 » Fri May 24, 2013 6:28 am

I am new to python and this is the first python program I am working on. I have a program that will keep monitoring the website for the particular data change. If the program detect the change it will perform some automation tasks. The problem is that It will be hung if the internet connection is lost since I use the web scraping method to get the data that I want. I want to implement an error handling mechanism to avoid the program hang. Let say when the program cannot access the website due to intermittent connection lost, I want it to attempt at some other times to get the data instead of being hung. Please see my code below. Thank you very much in advance.

Code: Select all
import u3
import time
import urllib2
import re
from BeautifulSoup import BeautifulSoup
from BeautifulSoup import NavigableString


cl1 = 0
cl2 = 0
TesterStat = 1

def Turn_On_Testers():
  global TesterStat
  d = u3.U3()
  STATE_REGISTER = 6000
  if TesterStat == 0:
    TesterStat = 1
    for i in range (16):
      STATE_REGISTER = 6000 + i
      d.writeRegister(STATE_REGISTER, 1)
      time.sleep(.5)
      d.writeRegister(STATE_REGISTER, 0)
      time.sleep(.5)
      d.writeRegister(STATE_REGISTER, 1)
      a = i + 1
      print " Tester", a,  "is on"   
      time.sleep(10)
  else:
    print " Testers are on already "
 
   
def Turn_Off_Testers():
  global TesterStat
  d = u3.U3()
  STATE_REGISTER = 6000
  if TesterStat == 1:
    TesterStat = 0
    for i in range (16):
      STATE_REGISTER = 6000 + i
      d.writeRegister(STATE_REGISTER, 1)
      time.sleep(.5)
      d.writeRegister(STATE_REGISTER, 0)
      time.sleep(.5)
      d.writeRegister(STATE_REGISTER, 1)
      a = i + 1
      print " Tester", a,  "is off"
      time.sleep(1)
  else:
    print " Testers are off already "

def Get_CL1():
 
  html = BeautifulSoup(urllib2.urlopen("http://xxxx").read())
  nav_string = html.find('b', text='SPK QC TIPS Current Change List number ')
  td1 = nav_string.findParents('td')[0]
  td2 = td1.findNextSibling('td')
  number = td2.find(text=re.compile("\d+"))
  CL1 = number
  global cl1
  cl1 = CL1
     
def Get_CL2():
 
  html = BeautifulSoup(urllib2.urlopen("http://xxxx").read())
  nav_string = html.find('b', text='SPK QC TIPS Current Change List number ')
  td1 = nav_string.findParents('td')[0]
  td2 = td1.findNextSibling('td')
  number = td2.find(text=re.compile("\d+"))
  CL2 = number
  global cl2
  cl2 = CL2
   
def Compare(cl1,cl2):
  if cl2 > cl1:
    print " Old CL is - ", cl1, " ----  ", " New CL is - ", cl2,
    Turn_Off_Testers()
    time.sleep(60)     # 60 seconds
    Turn_On_Testers()
    time.sleep(259200) # 3 days
  else:
    print " No new CL detected!!! Current CL is - ", cl1,
   # print cl1

 

def main():
 while True:
   time.sleep(15)
   Get_CL1()
   time.sleep(30)
   Get_CL2()
   Compare(cl1,cl2)
 
 
main()
AM181179
 
Posts: 1
Joined: Fri May 24, 2013 5:40 am

Re: How to protect program hang

Postby micseydel » Fri May 24, 2013 7:20 am

What line does your program hang on? Wherever it is, you probably want to look for documentation on a "timeout" for that function call.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1354
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: How to protect program hang

Postby setrofim » Fri May 24, 2013 7:41 am

Try specifying a timeout in urllib2.urlopen call
Code: Select all
urllib2.urlopen("http://xxxx", timeout=10)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 3 guests