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)
python类wait4()的实例源码
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)
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)
def test_hello_world(self):
pid = os.fork()
if pid == 0:
class MyAPI(object):
def hello(self, name):
s = "Hello, %s!" % name
print(s)
return s
rpc_server = RPCPeer()
print("Server's nl_pid is %s." % rpc_server.nl_pid)
rpc_server.register_functions_in_object(MyAPI())
rpc_server.run_server_forever()
else:
rpc_client = RPCPeer()
rpc_client.talk_to(pid).hello("Rayson Zhu")
os.wait4(pid, 0)
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)
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)
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)
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)
try:
self.response_pipe.close()
except EnvironmentError, e:
errors.append("error closing response pipe:\n%s" % e)
errors.append(str(e))
try:
# We don't really care about the exit status, but we do want to be
# sure it isn't still running.
# Even if there were errors closing the pipes, it's most likely that
# the subprocesses has exited.
pid, exit_status, rusage = os.wait4(self.subprocess.pid, 0)
self.exit_status = exit_status
self.resource_usage = rusage
except EnvironmentError, e:
errors.append(str(e))
if errors:
raise GtpTransportError("\n".join(errors))
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)
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)
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)
def execute(cmd, fin, fout, ferr, exclusive=False, single_thread=False):
fi = sp.PIPE
if fin != '':
fi = open(fin, 'r')
fo = open(fout, 'w')
fe = open(ferr, 'w')
if exclusive and single_thread: # requires root
cmd = 'chrt -f 99 ' + cmd
if single_thread: # bind to cpu #3 by default
cmd = 'taskset 1 ' + cmd
cmd = '/usr/bin/time -p -o _time_ ' + cmd
global proc
proc = sp.Popen(shlex.split(cmd), stdin=fi, stdout=fo, stderr=fe, env=os.environ.copy())
tm = time.time()
try:
ru = os.wait4(proc.pid, 0)
except:
raise
finally:
tm = time.time() - tm
proc = None
if fi != sp.PIPE:
fi.close()
fo.close()
fe.close()
#if ru[1] != 0:
# raise Exception('Command "{}" failed with exit code {}'.format(cmd, ru[1])
ru = list(ru) + [float(tm), [], cmd]
with open('_time_') as ft:
for l in ft:
l = l.strip().split()
try:
t = (l[0], float(l[1]))
ru[-2].append(t)
except ValueError:
pass
os.remove('_time_')
return ru
def run(self):
"""
Call this function at the end of your class's `__init__` function.
"""
stderr = os.path.abspath(os.path.join(self.outdir, self.name + '.log'))
if self.pipe:
self.args += ('|', self.pipe, '2>>'+stderr)
if self.gzip:
self.args += ('|', 'gzip', '1>', self.gzip)
else:
self.args.append('2>>'+stderr)
self.args.append('1>>'+stderr)
# Print timestamp to log
log = open(stderr, 'a')
log.write("[gloTK] timestamp={}\n".format(utils.timestamp()))
cmd = ' '.join(map(str, self.args))
print(cmd)
log.write(cmd)
start = time.time()
save_cwd = os.getcwd()
try:
utils.safe_mkdir(self.outdir)
os.chdir(self.outdir)
spawn_pid = os.spawnle(os.P_NOWAIT, self.shell, self.shell, '-c', cmd, self.env)
wait_pid, retcode, rusage = os.wait4(spawn_pid, 0)
if wait_pid != spawn_pid:
utils.die("could not wait for process %d: got %d" % (spawn_pid, wait_pid))
os.chdir(save_cwd)
except OSError as e:
utils.info(e)
utils.die("could not run wrapper for command:\n%s" % cmd)
elapsed = time.time() - start
retcode = os.WEXITSTATUS(retcode)
if (self.return_ok is not None) and (self.return_ok != retcode):
# Give some context to the non-zero return.
if os.path.isfile(stderr):
subprocess.call(['tail', '-3', stderr])
utils.die("non-zero return (%d) from command:\n%s" % (retcode, cmd))
log.close()