Forums

Crash of my script maximum recursion depth exceeded

Hi, I'm very new in python, I created a simple script that retrieves crypto prices every 30 seconds via an API. I started my script (on the bash console) last night for the first time and today after 7am my script stopped and I had this error message :

I thought at first there was a quota overrun but I don't think so because these message appeared:

  **[Previous line repeated 961 more times]**
**RecursionError: maximum recursion depth exceeded in comparison**

Do you have any idea what caused this stop of my script and how to remedy it? PS: Can we restart a batch console automatically if it crashes or only python console?

All the error message :

  Traceback (most recent call last):
      File "crypto_price_v1.py", line 54, in <module>
        get_data()
      File "crypto_price_v1.py", line 52, in get_data
        get_data()
      File "crypto_price_v1.py", line 52, in get_data
        get_data()
      File "crypto_price_v1.py", line 52, in get_data
        get_data()
      **[Previous line repeated 961 more times]**
      File "crypto_price_v1.py", line 12, in get_data
        res = requests.get(url)
      File "/usr/lib/python3.7/site-packages/requests/api.py", line 72, in get
        return request('get', url, params=params, **kwargs)
      File "/usr/lib/python3.7/site-packages/requests/api.py", line 58, in request
        return session.request(method=method, url=url, **kwargs)
      File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
        resp = self.send(prep, **send_kwargs)
      File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
        r = adapter.send(request, **kwargs)
      File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
        timeout=timeout
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
        chunked=chunked)
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
        self._validate_conn(conn)
      File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 849, in _validate_conn
        conn.connect()
      File "/usr/lib/python3.7/site-packages/urllib3/connection.py", line 367, in connect
        cert = self.sock.getpeercert()
      File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 351, in getpeercert
        'subjectAltName': get_subj_alt_name(x509)
      File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 202, in get_subj_alt_name
        ext = cert.extensions.get_extension_for_class(
      File "/usr/lib/python3.7/site-packages/cryptography/utils.py", line 161, in inner
        result = func(instance)
      File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/x509.py", line 134, in extensions
        self._backend, self._x509
      File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/decode_asn1.py", line 238, in parse
        value = handler(backend, ext_data)
      File "/usr/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/decode_asn1.py", line 417, in _decode_subject_alt_name
        _decode_general_names_extension(backend, ext)
      File "/usr/lib/python3.7/site-packages/cryptography/x509/extensions.py", line 1210, in __init__
        self._general_names = GeneralNames(general_names)
      File "/usr/lib/python3.7/site-packages/cryptography/x509/extensions.py", line 1163, in __init__
        if not all(isinstance(x, GeneralName) for x in general_names):
      File "/usr/lib/python3.7/site-packages/cryptography/x509/extensions.py", line 1163, in <genexpr>
        if not all(isinstance(x, GeneralName) for x in general_names):
      File "/usr/lib/python3.7/abc.py", line 139, in __instancecheck__
        return _abc_instancecheck(cls, instance)
      File "/usr/lib/python3.7/abc.py", line 143, in __subclasscheck__
        return _abc_subclasscheck(cls, subclass)
    **RecursionError: maximum recursion depth exceeded in comparison**

[edit by admin: formatting]

If you want something to run forever, restarting if it crashes for any reason, then the best place to set it up is as an always-on task on the "Tasks" page.

The RecursionError is being caused by a function that calls itself without end -- for example, you would get one with this (silly) example code:

def foo():
    foo()

foo()

Obviously the cause of the infinite recursion in the case of your code is likely to be a bit more subtle :-) Could you post the code for the get_data() function?

Hi thank you for your answer,

If you want something to run forever, restarting if it crashes for any reason, then the best place to set it up is as an always-on task on the "Tasks" page.

Ok you confirm me that it's working with bash console !

Here my code :

import requests
import datetime 
import time

cronjob = True
BTCusdLast = 0
XTZusdLast = 0

def get_data():
    app_key = 'DELETED!!!!'
    url = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC&tsyms=USD,EUR&api_key=' + app_key 
    res = requests.get(url)
    data = res.json()

    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    BTCusd = data['BTC']['USD']
    BTCeuro = data['BTC']['EUR']

    global BTCusdLast

    print ('---------------------------')
    print (now)
    print ('---------------------------')
    if BTCusdLast==BTCusd:
        print ('BTC : {}$'.format(BTCusd) + ' ({}€)'.format(BTCeuro))
    if BTCusdLast<BTCusd:
        print ('BTC : \033[32m{}\033[0m$'.format(BTCusd) + ' ({}€)'.format(BTCeuro))
    if BTCusdLast>BTCusd:
        print ('BTC : \033[31m{}\033[0m$'.format(BTCusd) + ' ({}€)'.format(BTCeuro))

    BTCusdLast = BTCusd

    if cronjob:
        time.sleep(30)
        get_data()

get_data()

that last "if cronjob" line will keep stacking up get_data calls until it crashes

That's the goal that my script is always running (24/24). But how to avoid messages:

RecursionError: maximum recursion depth exceeded in comparison

move it outside of the function itself

But if I do that my function will only be performed once!

EDIT : I will try with an infinite loop ;)

Right -- an infinite loop like this outside the function is probably what you want:

if cronjob:
    while True:
        get_data()
        time.sleep(30)

(Having removed the equivalent code from the function itself, of course!)