The error you are getting "maximum recursion depth exceeded" means that your the call stack is too deep. The depth of the call stack is basically how many functions were called from the initial call point(Something like your main function/loop) without returning.
So for example if from your initial call point you call func1()
. In func1()
the depth is currently 1. If func1()
, before returning calls another function, the depth in that function will be 2.
To find out what the current limit is on the size of the call stack call sys.getrecursionlimit()
and to change it, call sys.setrecursionlimit()
.
If the error is thrown when pickling the code, then chances are, the structure you are trying to pickle is has too many nested elements. This is because, the dump
/dumps
will recurse on containers in the structure being dumped. Meaning, if you're dumping a list, it will dump each element in the list which may themselves be a list or dict, so it will dump the elements those first and so on and so forth. If the elements are nested too deep(lists or dicts, which contain lists or dict, when contain lists or dicts ad infinitum) then at some point you will hit the maximum recursion depth.
The problem may be elsewhere, but without any further details that's the only thing that could be causing(if indeed that's where the error is thrown).
I have tried pickling a list of 1000 dicts(which are similar in structure to yours) and it worked just fine. Therefore either you're inadvertently building a structure that is too nested or that error is not getting thrown in the call to picke.dump()
Edit:
Try this function to see how nested the structure you're trying to dump is:
def nestedness(struct):
if isinstance(struct, list):
return max([0] + [nestedness(i) for i in struct]) + 1
if isinstance(struct, dict):
return max([0] + [nestedness(i) for i in struct.values()])+1
return 1
If it fails with maximum recursion depth exceeded, keep raising it using sys.setrecursionlimit()
until you get a proper answer. If your structure is just too nested see if you can find a way to make it less so by structuring it differently.
Edit: for posterity's sake, fixed nestedness so it handles empty dicts and sequences