Image for post
Image for post

Python resource limitation

It turns out that you can explicitly specify the number of resources for a particular application in Python. By resources, I mean CPU, memory, number of processes, number of open files, call stack, etc. Python’s resource module in the standard library gives you an easy way to do that and more.

An example of CPU time limitation:

import signal
import resource


Time's up!

In this program resource.setrlimit() is used to set a soft and hard limit on a certain resource.

A soft limit is a current limit, which can be reduced or increased by the process over time. When an operating system reaches the soft limit, it usually limits or notifies the process by a signal.
The hard limit is the upper limit of the values that can be used for the soft limit. It can only be lowered to the soft limit and can never be raised by the user process. To raise the hard limit, you must be a superuser.

Example of limiting the number of processes:

import signal
import resource
import multiprocessing


BlockingIOError: [Errno 11] Resource temporarily unavailable

It would be best to check the documentation page of the resource module to find out what other resources can be restricted.

Thank you for reading!

Any questions? Leave your comment below to start fantastic discussions!

Check out my blog or come to say hi 👋 on Twitter or subscribe to my telegram channel.
Plan your best!

helping robots conquer the earth and trying not to increase entropy using Python, Big Data, Machine Learning. Check out my blog —

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store