出错后重新启动python文件

Mac*_*acD 2 python

我有一个程序可以流式传输价格,但在运行缓慢的时段出现 badstatusline 错误。这会导致需要与流交互的其他文件出现问题。我在简单地捕获异常时遇到了很多麻烦,导致了由于某种原因我无法捕获的其他异常BadStatusLine导致CannotSendRequest导致ResponseNotReady。当execution.py 引发异常时,如何简单地重新启动(在本例中)trading.py BadStatusLine

这是我现在的处理方式..

while True:
    try:
        response = self.conn.getresponse().read()
        print response
    except Exception:
        pass 
    else:
        break
Run Code Online (Sandbox Code Playgroud)

如果这很重要的话,它是使用 Httplib 的流

谢谢您的帮助

这也是错误:

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: ''
Run Code Online (Sandbox Code Playgroud)

jay*_*tar 5

如果您连续归档,那么您可以将其放入主管并添加

auto_start = True 
Run Code Online (Sandbox Code Playgroud)

或者在你的代码中你可以做类似的事情

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

我添加了广泛的异常,因为您不知道发生了哪个异常