Python resource.RLIMIT_NPROC Examples

The following are 10 code examples of resource.RLIMIT_NPROC(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module resource , or try the search function .
Example #1
Source File: test_subprocess.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_preexec_fork_failure(self):
        # The internal code did not preserve the previous exception when
        # re-enabling garbage collection
        try:
            from resource import getrlimit, setrlimit, RLIMIT_NPROC
        except ImportError as err:
            self.skipTest(err)  # RLIMIT_NPROC is specific to Linux and BSD
        limits = getrlimit(RLIMIT_NPROC)
        [_, hard] = limits
        setrlimit(RLIMIT_NPROC, (0, hard))
        self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
        try:
            subprocess.call([sys.executable, '-c', ''],
                            preexec_fn=lambda: None)
        except BlockingIOError:
            # Forking should raise EAGAIN, translated to BlockingIOError
            pass
        else:
            self.skipTest('RLIMIT_NPROC had no effect; probably superuser') 
Example #2
Source File: util.py    From BugZoo with MIT License 6 votes vote down vote up
def report_resource_limits(logger: logging.Logger) -> None:
    resources = [
        ('CPU time (seconds)', resource.RLIMIT_CPU),
        ('Heap size (bytes)', resource.RLIMIT_DATA),
        ('Num. process', resource.RLIMIT_NPROC),
        ('Num. files', resource.RLIMIT_NOFILE),
        ('Address space', resource.RLIMIT_AS),
        ('Locked address space', resource.RLIMIT_MEMLOCK)
    ]
    resource_limits = [
        (name, resource.getrlimit(res)) for (name, res) in resources
    ]
    resource_s = '\n'.join([
        '* {}: {}'.format(res, lim) for (res, lim) in resource_limits
    ])
    logger.info("resource limits:\n%s", indent(resource_s, 2)) 
Example #3
Source File: test_subprocess.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_preexec_fork_failure(self):
        # The internal code did not preserve the previous exception when
        # re-enabling garbage collection
        try:
            from resource import getrlimit, setrlimit, RLIMIT_NPROC
        except ImportError as err:
            self.skipTest(err)  # RLIMIT_NPROC is specific to Linux and BSD
        limits = getrlimit(RLIMIT_NPROC)
        [_, hard] = limits
        setrlimit(RLIMIT_NPROC, (0, hard))
        self.addCleanup(setrlimit, RLIMIT_NPROC, limits)
        # Forking should raise EAGAIN, translated to BlockingIOError
        with self.assertRaises(BlockingIOError):
            subprocess.call([sys.executable, '-c', ''],
                            preexec_fn=lambda: None) 
Example #4
Source File: launcher.py    From opsbro with MIT License 5 votes vote down vote up
def __find_and_set_higer_system_limits(self):
        resource = get_resource_lib()
        if not resource:
            logger.info('System resource package is not available, cannot increase system limits')
            return
        for (res, res_name) in [(resource.RLIMIT_NPROC, 'number of process/threads'), (resource.RLIMIT_NOFILE, 'number of open files')]:
            self.__find_and_set_higer_system_limit(res, res_name) 
Example #5
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_number_processes(self):
        nprocs = self.soft_limit(resource.RLIMIT_NPROC, 1, 65535)
        prlimit = processutils.ProcessLimits(number_processes=nprocs)
        self.check_limit(prlimit, 'RLIMIT_NPROC', nprocs) 
Example #6
Source File: test_java_opts.py    From jvmquake with Apache License 2.0 5 votes vote down vote up
def thread_ulimit():
    import resource
    (x, y) = resource.getrlimit(resource.RLIMIT_NPROC)
    resource.setrlimit(
        resource.RLIMIT_NPROC,
        (x - 1000, y)
    )
    yield
    resource.setrlimit(resource.RLIMIT_NPROC, (x, y)) 
Example #7
Source File: test_basic_ooms.py    From jvmquake with Apache License 2.0 5 votes vote down vote up
def thread_ulimit():
    import resource
    (x, y) = resource.getrlimit(resource.RLIMIT_NPROC)
    resource.setrlimit(
        resource.RLIMIT_NPROC,
        (32768, y)
    )
    print("\nProcess limit after fixture: {}\n".format(
        resource.getrlimit(resource.RLIMIT_NPROC))
    )
    yield
    resource.setrlimit(resource.RLIMIT_NPROC, (x, y)) 
Example #8
Source File: server.py    From qinling with Apache License 2.0 5 votes vote down vote up
def _set_ulimit():
    """Limit resources usage for the current process and/or its children.

    Refer to https://docs.python.org/2.7/library/resource.html
    """
    customized_limits = {
        resource.RLIMIT_NOFILE: 1024,
        resource.RLIMIT_NPROC: 128,
        # TODO(lxkong): 50M by default, need to be configurable in future.
        resource.RLIMIT_FSIZE: 524288000
    }
    for t, soft in customized_limits.items():
        _, hard = resource.getrlimit(t)
        resource.setrlimit(t, (soft, hard)) 
Example #9
Source File: server.py    From qinling with Apache License 2.0 5 votes vote down vote up
def _set_ulimit():
    """Limit resources usage for the current process and/or its children.

    Refer to https://docs.python.org/2.7/library/resource.html
    """
    customized_limits = {
        resource.RLIMIT_NOFILE: 1024,
        resource.RLIMIT_NPROC: 128,
        # TODO(lxkong): 50M by default, need to be configurable in future.
        resource.RLIMIT_FSIZE: 524288000
    }
    for t, soft in list(customized_limits.items()):
        _, hard = resource.getrlimit(t)
        resource.setrlimit(t, (soft, hard)) 
Example #10
Source File: autograder.py    From autograder with GNU General Public License v3.0 4 votes vote down vote up
def setProcessLimits(x):
        # This is called after fork and before exec. Any messages
        # printed here will look like the program that we are calling
        # printed them out.

        #print("pre switch user")
        if switchUser:
            if os.geteuid() != 0:
                print("We can't switch to a different user if this script is not run as root.")
                exit(1)
            # If we are supposed to switch to a different user account to do the grading
            if os.geteuid() == 0:
                os.setreuid(autograderUid,autograderUid)
            if os.geteuid() == 0:
                print("Still root after trying to switch to autograder user?")
                exit(1)
        else:
            # If we are not switching to a different user, make sure that we don't run as root.
            if os.geteuid() == 0:
                print("Halting. Do not run submitted programs as root.")
                exit(1)
                
        #print("post switch user")
        
        #print("Preexec start")
        if os.setpgrp() == -1:  # put all processes in the same process group so we can kill it and all children it creates.
            print("Failed to set process group!")
        #print("Preexec middle")

        def limitHelper(limitType, limit):
            # limit is a string referring to a previously set environment variable.
            if limit in os.environ:
                limit = int(os.environ[limit])
                (soft, hard) = resource.getrlimit(limitType)
                #print("soft %d, hard %d, requested %d\n" % (soft, hard, limit))
                if hard > 0 and limit > hard:
                    limit = hard
                resource.setrlimit(limitType, (limit, limit))

        limitHelper(resource.RLIMIT_NPROC, "ULIMIT_NPROC")
        limitHelper(resource.RLIMIT_AS, "ULIMIT_AS")
        limitHelper(resource.RLIMIT_DATA, "ULIMIT_DATA")