Capturing a MemoryError in Python?

Is there a way to globally throw MemoryError exceptions so that the library can clear the caches rather than letting MemoryError see the user code?

I am developing a memory caching library in Python that stores very large objects, to the extent that users usually need to use all available RAM to simplify their scripts and / or speed them up. I would like to have a hook where the python interpreter requests a callback function to free some RAM as a way to avoid calling a MemoryError in the user code.

OS: Solaris and / or Linux

Python: cPython 2.6. *


EDIT: I am looking for a mechanism that will not be processed except for the block. If for any reason a memory error occurred in any code, I would like the Python interpreter to try to use the callback to release some RAM and never throw a MemoryError exception. I do not control the code that will generate errors, and I would like my cache to aggressively use as much RAM as it wants, automatically freeing up RAM as necessary in the user code.

+3
source share
3 answers

. , MemoryError, , , , , , , OOM- , .

, , , MemoryError, - , , 32- .

, , , . , , , , . , , , , , .

, , , , .

+4

A MemoryError , except.

+1

... MemoryException , , , . , ulimit . , , , (, , , ).

, - , , . , , , ! , ( ) .

:

sys.excepthook = <your_exceptionhook>

- , . , traceback.format_exception(), python stderr .

NB: , MemoryException, .

+1
source

Source: https://habr.com/ru/post/1770794/


All Articles