本文整理汇总了Python中os.set_inheritable方法的典型用法代码示例。如果您正苦于以下问题:Python os.set_inheritable方法的具体用法?Python os.set_inheritable怎么用?Python os.set_inheritable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.set_inheritable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sendfile
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def sendfile(self, file, offset=0, count=None):
"""sendfile(file[, offset[, count]]) -> sent
Send a file until EOF is reached by using high-performance
os.sendfile() and return the total number of bytes which
were sent.
*file* must be a regular file object opened in binary mode.
If os.sendfile() is not available (e.g. Windows) or file is
not a regular file socket.send() will be used instead.
*offset* tells from where to start reading the file.
If specified, *count* is the total number of bytes to transmit
as opposed to sending the file until EOF is reached.
File position is updated on return or also in case of error in
which case file.tell() can be used to figure out the number of
bytes which were sent.
The socket must be of SOCK_STREAM type.
Non-blocking sockets are not supported.
.. versionadded:: 1.1rc4
Added in Python 3.5, but available under all Python 3 versions in
gevent.
"""
return self._sendfile_use_send(file, offset, count)
# get/set_inheritable new in 3.4
示例2: test_pass_fds_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def test_pass_fds_inheritable(self):
script = support.findfile("fd_status.py", subdir="subprocessdata")
inheritable, non_inheritable = os.pipe()
self.addCleanup(os.close, inheritable)
self.addCleanup(os.close, non_inheritable)
os.set_inheritable(inheritable, True)
os.set_inheritable(non_inheritable, False)
pass_fds = (inheritable, non_inheritable)
args = [sys.executable, script]
args += list(map(str, pass_fds))
p = subprocess.Popen(args,
stdout=subprocess.PIPE, close_fds=True,
pass_fds=pass_fds)
output, ignored = p.communicate()
fds = set(map(int, output.split(b',')))
# the inheritable file descriptor must be inherited, so its inheritable
# flag must be set in the child process after fork() and before exec()
self.assertEqual(fds, set(pass_fds), "output=%a" % output)
# inheritable flag must not be changed in the parent process
self.assertEqual(os.get_inheritable(inheritable), True)
self.assertEqual(os.get_inheritable(non_inheritable), False)
示例3: set_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def set_inheritable(fd, inheritable):
# On py34+ we can use os.set_inheritable but < py34 we must polyfill
# with fcntl and SetHandleInformation
if hasattr(os, 'get_inheritable'):
if os.get_inheritable(fd) != inheritable:
os.set_inheritable(fd, inheritable)
elif WIN:
h = get_handle(fd)
flags = winapi.HANDLE_FLAG_INHERIT if inheritable else 0
winapi.SetHandleInformation(h, winapi.HANDLE_FLAG_INHERIT, flags)
else:
flags = fcntl.fcntl(fd, fcntl.F_GETFD)
if inheritable:
new_flags = flags & ~fcntl.FD_CLOEXEC
else:
new_flags = flags | fcntl.FD_CLOEXEC
if new_flags != flags:
fcntl.fcntl(fd, fcntl.F_SETFD, new_flags)
示例4: spawn
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def spawn(spec, kwargs, pass_fds=()):
"""
Invoke a python function in a subprocess.
"""
r, w = os.pipe()
for fd in [r] + list(pass_fds):
set_inheritable(fd, True)
preparation_data = get_preparation_data()
r_handle = get_handle(r)
args, env = get_command_line(pipe_handle=r_handle)
process = subprocess.Popen(args, env=env, close_fds=False)
to_child = os.fdopen(w, 'wb')
to_child.write(pickle.dumps([preparation_data, spec, kwargs]))
to_child.close()
return process
示例5: run
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def run(cmd, close_in_child, keep_in_child, with_pgrp=True):
child = os.fork()
if child:
return child
if with_pgrp:
os.setpgrp() # this pgrp is killed if the job fails
for fd in close_in_child:
os.close(fd)
keep_in_child = set(keep_in_child)
keep_in_child.add(int(os.getenv('BD_STATUS_FD')))
keep_in_child.add(int(os.getenv('BD_TERM_FD')))
close_fds(keep_in_child)
# unreadable stdin - less risk of stuck jobs
devnull = os.open('/dev/null', os.O_RDONLY)
os.dup2(devnull, 0)
os.close(devnull)
if PY3:
keep_in_child.update([1, 2])
for fd in keep_in_child:
os.set_inheritable(fd, True)
os.execv(cmd[0], cmd)
os._exit()
示例6: set_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def set_inheritable(self, inheritable):
os.set_handle_inheritable(self.fileno(), inheritable)
示例7: _close_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def _close_fds(self, keep):
# `keep` is a set of fds, so we
# use os.closerange from 3 to min(keep)
# and then from max(keep + 1) to MAXFD and
# loop through filling in the gaps.
# Under new python versions, we need to explicitly set
# passed fds to be inheritable or they will go away on exec
if hasattr(os, 'set_inheritable'):
set_inheritable = os.set_inheritable
else:
set_inheritable = lambda i, v: True
if hasattr(os, 'closerange'):
keep = sorted(keep)
min_keep = min(keep)
max_keep = max(keep)
os.closerange(3, min_keep)
os.closerange(max_keep + 1, MAXFD)
for i in xrange(min_keep, max_keep):
if i in keep:
set_inheritable(i, True)
continue
try:
os.close(i)
except:
pass
else:
for i in xrange(3, MAXFD):
if i in keep:
set_inheritable(i, True)
continue
try:
os.close(i)
except:
pass
示例8: set_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def set_inheritable(self, inheritable):
os.set_handle_inheritable(self.fileno(), inheritable)
示例9: test_pass_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def test_pass_fds(self):
fd_status = support.findfile("fd_status.py", subdir="subprocessdata")
open_fds = set()
for x in range(5):
fds = os.pipe()
self.addCleanup(os.close, fds[0])
self.addCleanup(os.close, fds[1])
os.set_inheritable(fds[0], True)
os.set_inheritable(fds[1], True)
open_fds.update(fds)
for fd in open_fds:
p = subprocess.Popen([sys.executable, fd_status],
stdout=subprocess.PIPE, close_fds=True,
pass_fds=(fd, ))
output, ignored = p.communicate()
remaining_fds = set(map(int, output.split(b',')))
to_be_closed = open_fds - {fd}
self.assertIn(fd, remaining_fds, "fd to be passed not passed")
self.assertFalse(remaining_fds & to_be_closed,
"fd to be closed passed")
# pass_fds overrides close_fds with a warning.
with self.assertWarns(RuntimeWarning) as context:
self.assertFalse(subprocess.call(
[sys.executable, "-c", "import sys; sys.exit(0)"],
close_fds=False, pass_fds=(fd, )))
self.assertIn('overriding close_fds', str(context.warning))
示例10: test_get_set_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def test_get_set_inheritable(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)
self.assertEqual(os.get_inheritable(fd), False)
os.set_inheritable(fd, True)
self.assertEqual(os.get_inheritable(fd), True)
示例11: test_set_inheritable_cloexec
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def test_set_inheritable_cloexec(self):
fd = os.open(__file__, os.O_RDONLY)
self.addCleanup(os.close, fd)
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
fcntl.FD_CLOEXEC)
os.set_inheritable(fd, True)
self.assertEqual(fcntl.fcntl(fd, fcntl.F_GETFD) & fcntl.FD_CLOEXEC,
0)
示例12: _pipe
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def _pipe():
r, w = os.pipe()
set_inheritable(r, False)
set_inheritable(w, False)
return r, w
示例13: listen
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def listen(self, port=8000, host="127.0.0.1", workers=multiprocessing.cpu_count()):
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
pid = os.getpid()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(False)
sock.bind((host, port))
os.set_inheritable(sock.fileno(), True)
try:
print(f'[{pid}] Listening at: http://{host}:{port}')
print(f'[{pid}] Workers: {workers}')
for _ in range(workers):
worker = multiprocessing.Process(target=self.serve, kwargs=dict(sock=sock))
worker.daemon = True
worker.start()
print(f'[{pid}] Starting worker with pid: {worker.pid}')
self.workers.add(worker)
for worker in self.workers:
worker.join()
except KeyboardInterrupt:
print('\r', end='\r')
print(f'[{pid}] Server soft stopping')
for worker in self.workers:
worker.terminate()
worker.join()
print(f'[{pid}] Server stopped successfully!')
sock.close()
示例14: _mk_inheritable
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def _mk_inheritable(fd):
if sys.version_info[:2] > (3, 3):
os.set_inheritable(fd, True)
return fd
示例15: __init__
# 需要导入模块: import os [as 别名]
# 或者: from os import set_inheritable [as 别名]
def __init__(self, job_name, log_dir):
self.finished = Deferred()
self.out_path = os.path.join(log_dir, job_name + '.out')
self.err_path = os.path.join(log_dir, job_name + '.err')
flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
self.out_fd = os.open(self.out_path, flags, 0o644)
self.err_fd = os.open(self.err_path, flags, 0o644)
os.set_inheritable(self.out_fd, True)
os.set_inheritable(self.err_fd, True)
#---------------------------------------------------------------------------