Python resource.getrlimit() Examples
The following are 30
code examples of resource.getrlimit().
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
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_os.py From ironpython2 with Apache License 2.0 | 7 votes |
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
Example #2
Source File: core.py From daemonocle with MIT License | 7 votes |
def _reset_file_descriptors(self): """Close open file descriptors and redirect standard streams.""" if self.close_open_files: # Attempt to determine the max number of open files max_fds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if max_fds == resource.RLIM_INFINITY: # If the limit is infinity, use a more reasonable limit max_fds = 2048 else: # If we're not closing all open files, we at least need to # reset STDIN, STDOUT, and STDERR. max_fds = 3 for fd in range(max_fds): try: os.close(fd) except OSError: # The file descriptor probably wasn't open pass # Redirect STDIN, STDOUT, and STDERR to /dev/null devnull_fd = os.open(os.devnull, os.O_RDWR) os.dup2(devnull_fd, 0) os.dup2(devnull_fd, 1) os.dup2(devnull_fd, 2)
Example #3
Source File: daemon.py From virt-who with GNU General Public License v2.0 | 6 votes |
def get_maximum_file_descriptors(): """ Return the maximum number of open file descriptors for this process. Return the process hard resource limit of maximum number of open file descriptors. If the limit is “infinity”, a default value of ``MAXFD`` is returned. """ limits = resource.getrlimit(resource.RLIMIT_NOFILE) result = limits[1] if result == resource.RLIM_INFINITY: result = MAXFD return result
Example #4
Source File: daemon.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def prevent_core_dump(): """ Prevent this process from generating a core dump. Sets the soft and hard limits for core dump size to zero. On Unix, this prevents the process from creating core dump altogether. """ core_resource = resource.RLIMIT_CORE try: # Ensure the resource limit exists on this platform, by requesting # its current value core_limit_prev = resource.getrlimit(core_resource) except ValueError, exc: error = DaemonOSEnvironmentError( "System does not support RLIMIT_CORE resource limit (%(exc)s)" % vars()) raise error # Set hard and soft limits to zero, i.e. no core dump at all
Example #5
Source File: daemon.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def get_maximum_file_descriptors(): """ Return the maximum number of open file descriptors for this process. Return the process hard resource limit of maximum number of open file descriptors. If the limit is “infinity”, a default value of ``MAXFD`` is returned. """ limits = resource.getrlimit(resource.RLIMIT_NOFILE) result = limits[1] if result == resource.RLIM_INFINITY: result = MAXFD return result
Example #6
Source File: test_os.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
Example #7
Source File: util.py From Flask-P2P with MIT License | 6 votes |
def get_maxfd(): maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD return maxfd
Example #8
Source File: process.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def _fallbackFDImplementation(self): """ Fallback implementation where either the resource module can inform us about the upper bound of how many FDs to expect, or where we just guess a constant maximum if there is no resource module. All possible file descriptors from 0 to that upper bound are returned with no attempt to exclude invalid file descriptor values. """ try: import resource except ImportError: maxfds = 1024 else: # OS-X reports 9223372036854775808. That's a lot of fds to close. # OS-X should get the /dev/fd implementation instead, so mostly # this check probably isn't necessary. maxfds = min(1024, resource.getrlimit(resource.RLIMIT_NOFILE)[1]) return range(maxfds)
Example #9
Source File: test_subprocess.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __enter__(self): """Try to save previous ulimit, then set it to (0, 0).""" if resource is not None: try: self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) except (ValueError, resource.error): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print "this tests triggers the Crash Reporter, that is intentional" sys.stdout.flush()
Example #10
Source File: utility.py From MIDAS with GNU General Public License v3.0 | 6 votes |
def batch_samples(samples, threads): """ Split up samples into batches assert: batch_size * threads < max_open assert: len(batches) == threads """ import resource import math max_open = int(0.8 * resource.getrlimit(resource.RLIMIT_NOFILE)[0]) # max open files on system max_size = math.floor(max_open/threads) # max batch size to avoid exceeding max_open min_size = math.ceil(len(samples)/float(threads)) # min batch size to use all threads size = min(min_size, max_size) batches = [] batch = [] for sample in samples: batch.append(sample) if len(batch) >= size: batches.append(batch) batch = [] if len(batch) > 0: batches.append(batch) return batches
Example #11
Source File: test_os.py From oss-ftp with MIT License | 6 votes |
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
Example #12
Source File: test_subprocess.py From oss-ftp with MIT License | 6 votes |
def __enter__(self): """Try to save previous ulimit, then set it to (0, 0).""" if resource is not None: try: self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) except (ValueError, resource.error): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print "this tests triggers the Crash Reporter, that is intentional" sys.stdout.flush()
Example #13
Source File: test_os.py From gcblue with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
Example #14
Source File: daemon.py From virt-who with GNU General Public License v2.0 | 6 votes |
def prevent_core_dump(): """ Prevent this process from generating a core dump. Sets the soft and hard limits for core dump size to zero. On Unix, this prevents the process from creating core dump altogether. """ core_resource = resource.RLIMIT_CORE try: # Ensure the resource limit exists on this platform, by requesting # its current value resource.getrlimit(core_resource) except ValueError as exc: error = DaemonOSEnvironmentError( "System does not support RLIMIT_CORE resource limit (%s)" % exc) raise error # Set hard and soft limits to zero, i.e. no core dump at all core_limit = (0, 0) resource.setrlimit(core_resource, core_limit)
Example #15
Source File: test_subprocess.py From BinderFilter with MIT License | 6 votes |
def __enter__(self): """Try to save previous ulimit, then set it to (0, 0).""" if resource is not None: try: self.old_limit = resource.getrlimit(resource.RLIMIT_CORE) resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) except (ValueError, resource.error): pass if sys.platform == 'darwin': # Check if the 'Crash Reporter' on OSX was configured # in 'Developer' mode and warn that it will get triggered # when it is. # # This assumes that this context manager is used in tests # that might trigger the next manager. value = subprocess.Popen(['/usr/bin/defaults', 'read', 'com.apple.CrashReporter', 'DialogType'], stdout=subprocess.PIPE).communicate()[0] if value.strip() == b'developer': print "this tests triggers the Crash Reporter, that is intentional" sys.stdout.flush()
Example #16
Source File: lib.py From edgedb with Apache License 2.0 | 6 votes |
def get_max_fileno(default: int=2048): """Return the maximum number of open file descriptors.""" limit = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if limit == resource.RLIM_INFINITY: return default return limit
Example #17
Source File: fork_exec.py From loky with BSD 3-Clause "New" or "Revised" License | 6 votes |
def close_fds(keep_fds): # pragma: no cover """Close all the file descriptors except those in keep_fds.""" # Make sure to keep stdout and stderr open for logging purpose keep_fds = set(keep_fds).union([1, 2]) # We try to retrieve all the open fds try: open_fds = set(int(fd) for fd in os.listdir('/proc/self/fd')) except FileNotFoundError: import resource max_nfds = resource.getrlimit(resource.RLIMIT_NOFILE)[0] open_fds = set(fd for fd in range(3, max_nfds)) open_fds.add(0) for i in open_fds - keep_fds: try: os.close(i) except OSError: pass
Example #18
Source File: test_process.py From psutil with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_rlimit_get(self): import resource p = psutil.Process(os.getpid()) names = [x for x in dir(psutil) if x.startswith('RLIMIT')] assert names, names for name in names: value = getattr(psutil, name) self.assertGreaterEqual(value, 0) if name in dir(resource): self.assertEqual(value, getattr(resource, name)) # XXX - On PyPy RLIMIT_INFINITY returned by # resource.getrlimit() is reported as a very big long # number instead of -1. It looks like a bug with PyPy. if PYPY: continue self.assertEqual(p.rlimit(value), resource.getrlimit(value)) else: ret = p.rlimit(value) self.assertEqual(len(ret), 2) self.assertGreaterEqual(ret[0], -1) self.assertGreaterEqual(ret[1], -1)
Example #19
Source File: process.py From learn_python3_spider with MIT License | 6 votes |
def _fallbackFDImplementation(self): """ Fallback implementation where either the resource module can inform us about the upper bound of how many FDs to expect, or where we just guess a constant maximum if there is no resource module. All possible file descriptors from 0 to that upper bound are returned with no attempt to exclude invalid file descriptor values. """ try: import resource except ImportError: maxfds = 1024 else: # OS-X reports 9223372036854775808. That's a lot of fds to close. # OS-X should get the /dev/fd implementation instead, so mostly # this check probably isn't necessary. maxfds = min(1024, resource.getrlimit(resource.RLIMIT_NOFILE)[1]) return range(maxfds)
Example #20
Source File: _daemonize_unix.py From py_daemoniker with The Unlicense | 6 votes |
def _autoclose_files(shielded=None, fallback_limit=1024): ''' Automatically close any open file descriptors. shielded is iterable of file descriptors. ''' # Process shielded. shielded = default_to(shielded, []) # Figure out the maximum number of files to try to close. # This returns a tuple of softlimit, hardlimit; the hardlimit is always # greater. softlimit, hardlimit = resource.getrlimit(resource.RLIMIT_NOFILE) # If the hard limit is infinity, we can't iterate to it. if hardlimit == resource.RLIM_INFINITY: # Check the soft limit. If it's also infinity, fallback to guess. if softlimit == resource.RLIM_INFINITY: fdlimit = fallback_limit # The soft limit is finite, so fallback to that. else: fdlimit = softlimit # The hard limit is not infinity, so prefer it. else: fdlimit = hardlimit # Skip fd 0, 1, 2, which are used by stdin, stdout, and stderr # (respectively) ranges_to_close = _make_range_tuples( start = 3, stop = fdlimit, exclude = shielded ) for start, stop in ranges_to_close: # How nice of os to include this for us! os.closerange(start, stop)
Example #21
Source File: util.py From jbox with MIT License | 6 votes |
def get_maxfd(): maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1] if (maxfd == resource.RLIM_INFINITY): maxfd = MAXFD return maxfd
Example #22
Source File: __main__.py From aws_list_all with MIT License | 6 votes |
def increase_limit_nofiles(): soft_limit, hard_limit = getrlimit(RLIMIT_NOFILE) desired_limit = 6000 # This should be comfortably larger than the product of services and regions if hard_limit < desired_limit: print("-" * 80, file=stderr) print( "WARNING!\n" "Your system limits the number of open files and network connections to {}.\n" "This may lead to failures during querying.\n" "Please increase the hard limit of open files to at least {}.\n" "The configuration for hard limits is often found in /etc/security/limits.conf".format( hard_limit, desired_limit ), file=stderr ) print("-" * 80, file=stderr) print(file=stderr) target_soft_limit = min(desired_limit, hard_limit) if target_soft_limit > soft_limit: print("Increasing the open connection limit \"nofile\" from {} to {}.".format(soft_limit, target_soft_limit)) setrlimit(RLIMIT_NOFILE, (target_soft_limit, hard_limit)) print("")
Example #23
Source File: timeout.py From Yuki-Chan-The-Auto-Pentest with MIT License | 6 votes |
def limitedTime(second, func, *args, **kw): second = fixTimeout(second) old_alarm = signal(SIGXCPU, signalHandler) current = getrlimit(RLIMIT_CPU) try: setrlimit(RLIMIT_CPU, (second, current[1])) return func(*args, **kw) finally: setrlimit(RLIMIT_CPU, current) signal(SIGXCPU, old_alarm)
Example #24
Source File: test_process.py From vnpy_crypto with MIT License | 6 votes |
def test_rlimit_get(self): import resource p = psutil.Process(os.getpid()) names = [x for x in dir(psutil) if x.startswith('RLIMIT')] assert names, names for name in names: value = getattr(psutil, name) self.assertGreaterEqual(value, 0) if name in dir(resource): self.assertEqual(value, getattr(resource, name)) # XXX - On PyPy RLIMIT_INFINITY returned by # resource.getrlimit() is reported as a very big long # number instead of -1. It looks like a bug with PyPy. if PYPY: continue self.assertEqual(p.rlimit(value), resource.getrlimit(value)) else: ret = p.rlimit(value) self.assertEqual(len(ret), 2) self.assertGreaterEqual(ret[0], -1) self.assertGreaterEqual(ret[1], -1)
Example #25
Source File: test_setup.py From avocado-vt with GNU General Public License v2.0 | 6 votes |
def _set(self): self.ulimit = {} for key in self.ulimit_options: set_value = self.params.get("vt_ulimit_%s" % key) if not set_value: continue # get default ulimit values in tuple (soft, hard) self.ulimit[key] = resource.getrlimit(self.ulimit_options[key]) logging.info("Setting ulimit %s to %s." % (key, set_value)) if set_value == "ulimited": set_value = resource.RLIM_INFINITY elif set_value.isdigit(): set_value = int(set_value) else: self.test.error("%s is not supported for " "setting ulimit %s" % (set_value, key)) try: resource.setrlimit(self.ulimit_options[key], (set_value, set_value)) except ValueError as error: self.test.error(str(error))
Example #26
Source File: test_os.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_urandom_failure(self): # Check urandom() failing when it is not able to open /dev/random. # We spawn a new process to make the test more robust (if getrlimit() # failed to restore the file descriptor limit after this, the whole # test suite would crash; this actually happened on the OS X Tiger # buildbot). code = """if 1: import errno import os import resource soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE) resource.setrlimit(resource.RLIMIT_NOFILE, (1, hard_limit)) try: os.urandom(16) except OSError as e: assert e.errno == errno.EMFILE, e.errno else: raise AssertionError("OSError not raised") """ assert_python_ok('-c', code)
Example #27
Source File: test_tcp_internals.py From learn_python3_spider with MIT License | 5 votes |
def tearDown(self): while self.openSockets: self.openSockets.pop().close() if resource is not None: # `macOS` implicitly lowers the hard limit in the setrlimit call # above. Retrieve the new hard limit to pass in to this # setrlimit call, so that it doesn't give us a permission denied # error. currentHardLimit = resource.getrlimit(resource.RLIMIT_NOFILE)[1] newSoftLimit = min(self.originalFileLimit[0], currentHardLimit) resource.setrlimit(resource.RLIMIT_NOFILE, (newSoftLimit, currentHardLimit))
Example #28
Source File: env.py From lbry-sdk with MIT License | 5 votes |
def sane_max_sessions(self): """Return the maximum number of sessions to permit. Normally this is MAX_SESSIONS. However, to prevent open file exhaustion, ajdust downwards if running with a small open file rlimit.""" env_value = self.integer('MAX_SESSIONS', 1000) nofile_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[0] # We give the DB 250 files; allow ElectrumX 100 for itself value = max(0, min(env_value, nofile_limit - 350)) if value < env_value: self.logger.warning(f'lowered maximum sessions from {env_value:,d} to {value:,d} ' f'because your open file limit is {nofile_limit:,d}') return value
Example #29
Source File: test_selectors.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_above_fd_setsize(self): # A scalable implementation should have no problem with more than # FD_SETSIZE file descriptors. Since we don't know the value, we just # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling. soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) try: resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE, (soft, hard)) NUM_FDS = min(hard, 2**16) except (OSError, ValueError): NUM_FDS = soft # guard for already allocated FDs (stdin, stdout...) NUM_FDS -= 32 s = self.SELECTOR() self.addCleanup(s.close) for i in range(NUM_FDS // 2): try: rd, wr = self.make_socketpair() except OSError: # too many FDs, skip - note that we should only catch EMFILE # here, but apparently *BSD and Solaris can fail upon connect() # or bind() with EADDRNOTAVAIL, so let's be safe self.skipTest("FD limit reached") try: s.register(rd, selectors.EVENT_READ) s.register(wr, selectors.EVENT_WRITE) except OSError as e: if e.errno == errno.ENOSPC: # this can be raised by epoll if we go over # fs.epoll.max_user_watches sysctl self.skipTest("FD limit reached") raise self.assertEqual(NUM_FDS // 2, len(s.select()))
Example #30
Source File: test_subprocess.py From ironpython3 with Apache License 2.0 | 5 votes |
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)