2

I have a program that streams prices and is getting a badstatusline error during slow hours. This causes issues with other files that need to interact with the stream. I am having much trouble simply catching the exceptions, leading to other exceptions that I cannot catch for some reason BadStatusLine leads to CannotSendRequest leads to ResponseNotReady. How can I simply restart (in this case) trading.py when execution.py raises the exception BadStatusLine?

Here is how I'm handling it now..

while True:
    try:
        response = self.conn.getresponse().read()
        print response
    except Exception:
        pass 
    else:
        break

Its a stream using Httplib if thats of importance

Thanks for the help

Here is the error as well:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/mattduhon/trading4.py", line 30, in trade
    execution.execute_order(event)
  File "/Users/mattduhon/execution.py", line 34, in execute_order
    response = self.conn.getresponse().read()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1073, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 415, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 379, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
MacD
  • 566
  • 3
  • 9
  • 27

2 Answers2

5

If you are file continuously then you can put it in supervisor and add

auto_start = True 

Or In your code you can do something like that

import os
while True:
            try:
                response = self.conn.getresponse().read()
                    print response
            except:
                os.system("python trading.py")

I added broad exception because you don't know which exception is occuring

jayprakashstar
  • 395
  • 2
  • 12
  • 1
    If this is trading.py itself, won't that accumulate a lot of python processes where everyone (except the last) is waiting for the next one to complete? If it is not, os.system is blocking iirc so the loop will stop. – swenzel May 22 '15 at 13:49
  • Its not trading.py, its execution.py, however, it seems this approach runs the program once then restarts it. Still better than getting an error and crashing i guess. – MacD May 22 '15 at 14:26
3

Create another script to run your main script, and try and except the whole thing:

try:
    execfile('main.py')

except:
    pass
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70