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: ''
2
  • 1
    httplib is not thread-safe: stackoverflow.com/questions/5825151/… Commented May 22, 2015 at 13:30
  • Thanks for the link, I might need to change to httplib3 then Commented May 22, 2015 at 13:35

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
3

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

try:
    execfile('main.py')

except:
    pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.