|
25 | 25 | from billiard.process import current_process
|
26 | 26 | except ImportError:
|
27 | 27 | current_process = None
|
| 28 | +from billiard.compat import get_fdmax, close_open_fds |
28 | 29 | # fileno used to be in this module
|
29 | 30 | from kombu.utils import maybe_fileno
|
30 | 31 | from kombu.utils.encoding import safe_str
|
31 | 32 | from contextlib import contextmanager
|
32 | 33 |
|
33 | 34 | from .local import try_import
|
34 |
| -from .five import items, range, reraise, string_t, zip_longest |
35 |
| -from .utils.functional import uniq |
| 35 | +from .five import items, reraise, string_t |
36 | 36 |
|
37 | 37 | _setproctitle = try_import('setproctitle')
|
38 | 38 | resource = try_import('resource')
|
@@ -110,26 +110,6 @@ class LockFailed(Exception):
|
110 | 110 | """Raised if a pidlock can't be acquired."""
|
111 | 111 |
|
112 | 112 |
|
113 |
| -def get_fdmax(default=None): |
114 |
| - """Return the maximum number of open file descriptors |
115 |
| - on this system. |
116 |
| -
|
117 |
| - :keyword default: Value returned if there's no file |
118 |
| - descriptor limit. |
119 |
| -
|
120 |
| - """ |
121 |
| - try: |
122 |
| - return os.sysconf('SC_OPEN_MAX') |
123 |
| - except: |
124 |
| - pass |
125 |
| - if resource is None: # Windows |
126 |
| - return default |
127 |
| - fdmax = resource.getrlimit(resource.RLIMIT_NOFILE)[1] |
128 |
| - if fdmax == resource.RLIM_INFINITY: |
129 |
| - return default |
130 |
| - return fdmax |
131 |
| - |
132 |
| - |
133 | 113 | class Pidfile(object):
|
134 | 114 | """Pidfile
|
135 | 115 |
|
@@ -268,30 +248,6 @@ def _create_pidlock(pidfile):
|
268 | 248 | return pidlock
|
269 | 249 |
|
270 | 250 |
|
271 |
| -if hasattr(os, 'closerange'): |
272 |
| - |
273 |
| - def close_open_fds(keep=None): |
274 |
| - # must make sure this is 0-inclusive (Issue #1882) |
275 |
| - keep = list(uniq(sorted( |
276 |
| - f for f in map(maybe_fileno, keep or []) if f is not None |
277 |
| - ))) |
278 |
| - maxfd = get_fdmax(default=2048) |
279 |
| - kL, kH = iter([-1] + keep), iter(keep + [maxfd]) |
280 |
| - for low, high in zip_longest(kL, kH): |
281 |
| - if low + 1 != high: |
282 |
| - os.closerange(low + 1, high) |
283 |
| - |
284 |
| -else: |
285 |
| - |
286 |
| - def close_open_fds(keep=None): # noqa |
287 |
| - keep = [maybe_fileno(f) |
288 |
| - for f in (keep or []) if maybe_fileno(f) is not None] |
289 |
| - for fd in reversed(range(get_fdmax(default=2048))): |
290 |
| - if fd not in keep: |
291 |
| - with ignore_errno(errno.EBADF): |
292 |
| - os.close(fd) |
293 |
| - |
294 |
| - |
295 | 251 | class DaemonContext(object):
|
296 | 252 | _is_open = False
|
297 | 253 |
|
|
0 commit comments