Skip to main content

A small Python library to limit the resources used by a function by executing it inside a subprocess.

Project description

The pynisher is a little module intended to limit a functions resources. It starts a new process, sets the desired limits, and executes the function inside it. In the end, it returns the function return value. If, for any reason, the function call is not successful, None is returned.

Currently, the total memory usage(*), the wall clock time, the cpu time and the number of processes can be limited.

(*) As the subprocess also includes the Python interpreter, the actual memory available to your function is less than the specified value.

import pynisher
import time

# using it as a decorator for every call to this function
@pynisher.enforce_limits(wall_time_in_s=2)
def my_function (t):
        time.sleep(t)
        return(t)

for t in range(5):
        print(my_function(t))

# a more explicit usage

def my_other_function(t):
        time.sleep(t)
        return(t)

# explicitly create a new function without wrapping the original everytime
my_wrapped_function = pynisher.enforce_limits(wall_time_in_s=3)(my_other_function)

for t in range(5):
        print(my_wrapped_function(t))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynisher-0.3.1.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

pynisher-0.3.1.linux-x86_64.tar.gz (4.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page