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(*), wall and cpu time, and the number of subprocesses can be limited.

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

To show the basic usage, consider the following script

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))

The full list of argments to enforce_limits reads:

pynisher.enforce_limits(mem_in_mb=None, cpu_time_in_s=None,\
                        wall_time_in_s=None, num_processes=None,\
                        grace_period_in_s = None, logger = None)

The first four are actual constraints on the memory, the CPU time, the wall time, and the number of subprocesses of the function. All values should be integers or None, which means no restriction.

The grace period allows the function to properly end. More technically, the subprocess receives a SIGXCPU/SIGALARM signal if the CPU/wall clock limit is reached. After the grace period a SIGKILL is send terminating the process immediately. Without a grace period, pynisher might not be able to correctly determine the cause of the shutdown, as the subprocess might die without any notice (more on that below).

The logger is used to display additional information about the status of the pynisher module (mostly debug level). This might come in handy while debugging.

If you need to know what happend to the function call or why it was aborted, you can use the object returned from pynisher.enforce_limits. Consider this slight variation of the above example:

import pynisher
import time

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

for t in range(5):
        obj = pynisher.enforce_limits(wall_time_in_s=2)(my_function)
        result = obj(t)
        print(result, obj.relut, obj.exit_status)

# see all the available information
print(vars(f))

The object `obj` can be used as the original function. After calling it, it contains the actual result, but also an indicator of what happend. The `exit_status` attribute is either zero (function returned properly) or one of the following exceptions:

pynisher.CpuTimeoutException    # CPU time limit was reached
pynisher.TimeoutException       # Wall clock time limit exceeded
pynisher.MemorylimitException   # function hit the memory constraint
pynisher.SubprocessException    # function tried to spawn too many subprocesses
pynisher.AnythingException      # Something else went wrong, e.g., your function received a signal and just died.

Here, the above issue about the grace period becomes interesting. Without it, it is likely that a AnythingException is returned where a Cpu-/TimeoutException would be appropriate.

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.4.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file pynisher-0.4.tar.gz.

File metadata

  • Download URL: pynisher-0.4.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pynisher-0.4.tar.gz
Algorithm Hash digest
SHA256 724e738e2e5d1d0a4053f73185957cda3097e100ee98ae02c6f779742ced5767
MD5 997727f1c8723ddbfc81246b2c8a6335
BLAKE2b-256 e01c6300893f6fc1d9ae72626978e7a3307d810e8419d8427aef5188bfc19c62

See more details on using hashes here.

Supported by

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