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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pynisher-0.3.1.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 530b3d1e3a71d09261164f21b92ffffd1b0d3b9a7ab1bea847a1a3980fae14e4 |
|
MD5 | c632ec3649d417f8696b50e357e8b992 |
|
BLAKE2b-256 | aba43c908aa2bd1f21f05ac8b8eda66f935b6a1ba434df2408f30efb52031c3e |