2

I have the following block in my python script that is running on an Ubuntu AWS EC2 instance:

try:
    data = json.loads(line)
    # further processing of data
except Exception, e:  
    # something went bad

line is a string ingested from a text file. In most cases it gets processed fine. From time to time, I get lines that are humongous. In this case the script dies ("-9") and dmesg -T shows a message like [Tue Jan 8 16:10:48 2013] Out of memory: Kill process 13609 (python) score 910 or sacrifice child

What I don't understand is why instead of crashing it does not catch an exception in the try-except block. And is it possible to make changes in this block so that the script does not crash but raises and exception? Thx

I Z
  • 5,719
  • 19
  • 53
  • 100

1 Answers1

1

Your Python process is being killed by the Kernel OOM (out-of-memory) killer (docs). This is killing Python with SIGKILL, so Python does not have any chance to respond to this event...hence it is not able to generate any exceptions for your code.

You may need to run your process in an environment with more resources (so that you're not using up such a large percentage of system memory), or you may need to place limits on the size of the JSON data you can read.

One of the links I posted below mentions ijson, which is an iterative JSON parser. This might be more memory efficient than the standard JSON parsers, but I haven't tried it.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Or he could try buffering the data, though that would be pretty tricky with something inherently structured like json. – Silas Ray Jan 08 '13 at 17:20
  • There are some suggestions [here](http://stackoverflow.com/questions/10382253/reading-rather-large-json-files-in-python) and [here](http://stackoverflow.com/questions/2400643/is-there-a-memory-efficient-and-fast-way-to-load-big-json-files-in-python) that might help. – larsks Jan 08 '13 at 17:21
  • Maybe there could be a way to query the available memory in the system in runtime and trigger an ad-hoc exception before the kernel. – Vichoko Nov 22 '19 at 00:15