I'm not going to post much code, and rather try to describe the issue I ran into. My program is taking a list of tuples in input:
input = [(a, b, c), (d, e, f, g)]
For each tuples in the list, a list of tuples is generated by my function f()
and dumped into a pickle file. This list can be small (1 element) but also very large (thousands, millions of elements). The larger .pkl file I got is about 9 Gb.
This step can be sump up like this:
for elt in input:
f(elt)
The function f()
actually calls different method / objects / functions and can take quite a large amount of RAM. It turns out, that I ran out of RAM for just a few of the elements in input. I would like to skip them and to go to the next when it happens (to avoid a program crash and to do the computation for the others input after the one that can't be done).
i.e.:
for elt in input:
try:
f(elt)
except:
continue
My problem is that I read that memory error are quite nasty and can't always be recovered from.
What is the best way to implement this try / except safety?
Is there a way to do this and purge the memory between the for
loops iteration?
EDIT for clarification:
One more point. I ran the program on a PC with 128 Gb of RAM. However, I did not run it only once. I ran it once in one thread on input1, once in another thread on input2, and so on. I had 10 of them running. The combination of the threads loaded too much in memory at a given instant. However, by going to the next iteration on some of the threads (with the safety), this should be avoided.
The main asset of this method (instead of reducing the number of thread) is that if I ran the program on a laptop with 8 Gb of RAM, in only one thread, it is still going to work. If one of the iteration needs more than the available RAM (which will happen on laptops with 8 Gb of RAM), it just skips it and go to the next.
Method seems straight forward to me, but I don't know how to implement that on python since the recovering of the memory error is not secure.