sixplus4iszen
sixplus4iszen

Reputation: 330

How to restart script on error with arguments?

I have my algo trading script which I want to run until cancelled but the script always crashes on below error which I don't know how to solve and most probably it's due to an unstable internet connection.

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.7/http/client.py", line 1321, in getresponse
    response.begin()
  File "/usr/lib/python3.7/http/client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.7/http/client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 299, in recv_into
    raise SocketError(str(e))
OSError: (104, 'ECONNRESET')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/lib/python3/dist-packages/six.py", line 692, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.7/http/client.py", line 1321, in getresponse
    response.begin()
  File "/usr/lib/python3.7/http/client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.7/http/client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 299, in recv_into
    raise SocketError(str(e))
urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Desktop/start_here/LiveStrategy-v20200102.py", line 349, in <module>
    continuousRun()
  File "/home/pi/Desktop/start_here/LiveStrategy-v20200102.py", line 340, in continuousRun
    Decider()
  File "/home/pi/Desktop/start_here/LiveStrategy-v20200102.py", line 287, in Decider
    getOpenPositions()
  File "/home/pi/Desktop/start_here/LiveStrategy-v20200102.py", line 206, in getOpenPositions
    openpositions = con.get_open_positions(kind='list')
  File "/home/pi/.local/lib/python3.7/site-packages/fxcmpy/fxcmpy.py", line 371, in get_open_positions
    data = self.get_model(('OpenPosition',))
  File "/home/pi/.local/lib/python3.7/site-packages/fxcmpy/fxcmpy.py", line 344, in get_model
    params={'models': list(models)})
  File "/home/pi/.local/lib/python3.7/site-packages/fxcmpy/fxcmpy.py", line 2477, in __handle_request__
    proxies=self.proxies)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

As visible from the error I run my code on Pi and I want the script to restart on error. So far I did that my trading script is run from another script with argument but it's not working and I don't know what the subprocess script is printing.

It should:

  1. restart on failure

  2. print everything from original script

My secondary script which I run with one argument which is passed on the initiated script by the subprocess.

import subprocess
import sys

symbol = sys.argv[1]
while True:
    proc = subprocess.Popen(['python', 'LiveStrategy-v20200102.py', symbol ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    proc.wait()
    print(proc)

Unfortunately this script does not restart on error and I don't understand why, I took this from some other answers here. And obviously it does not print from the subprocess script, because it's not implemented.

I want to keep it in Python so please no solutions with bash restarting the script.

Upvotes: 0

Views: 597

Answers (1)

Jortega
Jortega

Reputation: 3790

I am making it stop the loop with break after the counter hits a certain condition that you can set.

import subprocess
import sys

symbol = sys.argv[1]
counter = 0
while True:
    try:
        proc = subprocess.Popen(['python', 'LiveStrategy-v20200102.py', symbol], stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT)
        proc.wait()
        print(proc)
        break
    except:
        print("A")
        counter += 1
    if counter > 5:
        break

Upvotes: 1

Related Questions