本文整理汇总了Python中os.wait4方法的典型用法代码示例。如果您正苦于以下问题:Python os.wait4方法的具体用法?Python os.wait4怎么用?Python os.wait4使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.wait4方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wait_for_child_and_forward_signals
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def wait_for_child_and_forward_signals(child_pid, process_name):
"""Wait for a child to terminate and in the meantime forward all signals
that the current process receives to this child.
@return a tuple of exit code and resource usage of the child as given by os.waitpid
"""
block_all_signals()
while True:
logging.debug("Waiting for signals")
signum = signal.sigwait(_ALL_SIGNALS)
if signum == signal.SIGCHLD:
pid, exitcode, ru_child = os.wait4(-1, os.WNOHANG)
while pid != 0:
if pid == child_pid:
return exitcode, ru_child
else:
logging.debug("Received unexpected SIGCHLD for PID %s", pid)
pid, exitcode, ru_child = os.wait4(-1, os.WNOHANG)
else:
_forward_signal(signum, child_pid, process_name)
示例2: wait_impl
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def wait_impl(self, cpid):
option = os.WNOHANG
if sys.platform.startswith('aix'):
# Issue #11185: wait4 is broken on AIX and will always return 0
# with WNOHANG.
option = 0
for i in range(10):
# wait4() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait4(cpid, option)
if spid == cpid:
break
time.sleep(1.0)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)
示例3: wait_impl
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def wait_impl(self, cpid):
option = os.WNOHANG
if sys.platform.startswith('aix'):
# Issue #11185: wait4 is broken on AIX and will always return 0
# with WNOHANG.
option = 0
deadline = time.monotonic() + 10.0
while time.monotonic() <= deadline:
# wait4() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait4(cpid, option)
if spid == cpid:
break
time.sleep(0.1)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)
示例4: exec_child
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def exec_child(self, executable, environ=None, stdin=None, stdout=None, stderr=None, cwd=None):
'''Execute a child process with the environment set from the current environment, the
values of self.addtl_child_environ, the random numbers returned by self.random_val_env_vars, and
the given ``environ`` (applied in that order). stdin/stdout/stderr are optionally redirected.
This function waits on the child process to finish, then returns
(rc, rusage), where rc is the child's return code and rusage is the resource usage tuple from os.wait4()'''
all_environ = dict(os.environ)
all_environ.update(self.addtl_child_environ)
all_environ.update(self.random_val_env_vars())
all_environ.update(environ or {})
stdin = open(stdin, 'rb') if stdin else sys.stdin
stdout = open(stdout, 'wb') if stdout else sys.stdout
if stderr == 'stdout':
stderr = stdout
else:
stderr = open(stderr, 'wb') if stderr else sys.stderr
# close_fds is critical for preventing out-of-file errors
proc = subprocess.Popen([executable],
cwd = cwd,
stdin=stdin, stdout=stdout, stderr=stderr if stderr != stdout else subprocess.STDOUT,
close_fds=True, env=all_environ)
# Wait on child and get resource usage
(_pid, _status, rusage) = os.wait4(proc.pid, 0)
# Do a subprocess.Popen.wait() to let the Popen instance (and subprocess module) know that
# we are done with the process, and to get a more friendly return code
rc = proc.wait()
return (rc, rusage)
示例5: _wait_for_process
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def _wait_for_process(self, pid, name):
"""Wait for the given process to terminate.
@return tuple of exit code and resource usage
"""
try:
logging.debug("Waiting for process %s with pid %s", name, pid)
unused_pid, exitcode, ru_child = os.wait4(pid, 0)
return exitcode, ru_child
except OSError as e:
if self.PROCESS_KILLED and e.errno == errno.EINTR:
# Interrupted system call seems always to happen
# if we killed the process ourselves after Ctrl+C was pressed
# We can try again to get exitcode and resource usage.
logging.debug(
"OSError %s while waiting for termination of %s (%s): %s.",
e.errno,
name,
pid,
e.strerror,
)
try:
unused_pid, exitcode, ru_child = os.wait4(pid, 0)
return exitcode, ru_child
except OSError:
pass # original error will be handled and this ignored
logging.critical(
"OSError %s while waiting for termination of %s (%s): %s.",
e.errno,
name,
pid,
e.strerror,
)
return 0, None
示例6: wait_impl
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def wait_impl(self, cpid):
for i in range(10):
# wait4() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait4(cpid, os.WNOHANG)
if spid == cpid:
break
time.sleep(1.0)
self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage)
示例7: test_wait4
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def test_wait4(self):
self._test_wait_single(lambda pid: os.wait4(pid, 0))
示例8: close
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def close(self):
# Errors from closing pipes or wait4() are unlikely, but possible.
# Ideally would give up waiting after a while and forcibly terminate the
# subprocess.
errors = []
try:
self.command_pipe.close()
except EnvironmentError, e:
errors.append("error closing command pipe:\n%s" % e)
示例9: record_child_pid
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def record_child_pid(
pid: int, record_paths: "RecordPaths", timeout: Optional[int] = None
) -> Recording:
if timeout is None:
options = 0
else:
options = os.WNOHANG
record = RecordProcess(pid, record_paths)
with record:
ptrace_detach(pid)
start = time.time()
while True:
pid, exit_code, rusage = os.wait4(pid, options)
if pid != 0:
break
elif timeout is not None and time.time() - start <= 0:
raise TimeoutExpired(
"process did not finish within {} seconds".format(timeout)
)
time.sleep(0.10)
coredump, trace = record.result()
return Recording(coredump, trace, exit_code, rusage)
示例10: main
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def main() -> None:
result_file = Path(args.outdir).joinpath("result.json").resolve()
results: Dict[str, Any] = {args.name: {"original": [], "hase": []}}
for i in range(args.n):
results[args.name]["hase"].append(dict(run=i, result=[], valid=True))
with TemporaryDirectory() as tempdir:
temppath = Path(tempdir)
recording = record(
target=args.args,
record_path=temppath,
log_path=temppath.joinpath("logs"),
stdout=open(f"{args.outdir}/{args.name}_hase_{i}.out", "w"),
limit=1,
)
if recording:
rusage = recording.rusage
if rusage:
results[args.name]["hase"][i]["result"].append(list(rusage))
results[args.name]["original"].append(dict(run=i, result=[], valid=True))
process = subprocess.Popen(
args.args, stdout=open(f"{args.outdir}/{args.name}_{i}.out", "w")
)
_, _, rusage = os.wait4(process.pid, 0)
if rusage:
results[args.name]["original"][i]["result"].append(list(rusage))
with open(result_file, "w") as file:
json.dump(results, file, sort_keys=True, indent=4, separators=(",", ": "))
file.write("\n")
return
示例11: main
# 需要导入模块: import os [as 别名]
# 或者: from os import wait4 [as 别名]
def main():
args = get_options()
if args.show_id:
show_current_users_and_groups()
sys.exit(0)
mamaji_data = fetch_mamaji_data(args)
filter_options(mamaji_data)
target_cmd = None
if args.cmd:
target_cmd = args.cmd
if not args.do_fork:
change_users_and_groups(mamaji_data)
# if target_cmd is None, do nothing
if target_cmd:
os.execlp(target_cmd[0], *target_cmd)
sys.exit(0)
if args.do_fork and not args.cmd:
target_cmd = [find_shell()]
pid = os.fork()
if pid == -1:
warn('failed to do fork')
sys.exit(1)
elif pid == 0:
change_users_and_groups(mamaji_data)
os.execlp(target_cmd[0], *target_cmd)
else:
status = os.wait4(pid, 0)[1] >> 8
sys.exit(status)