本文整理汇总了Python中gevent.subprocess.Popen.poll方法的典型用法代码示例。如果您正苦于以下问题:Python Popen.poll方法的具体用法?Python Popen.poll怎么用?Python Popen.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gevent.subprocess.Popen
的用法示例。
在下文中一共展示了Popen.poll方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call_subprocess
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
def call_subprocess(self, cmd):
times = datetime.datetime.now()
# latest 14.0.4 requires "HOME" env variable to be passed
# copy current environment variables and add "HOME" variable
# pass the newly created environment variable to Popen subprocess
env_home = os.environ.copy()
env_home['HOME'] = HOME_ENV_PATH
# stdout and stderr are redirected.
# stderr not used (stdout validation is done so stderr check is
# is not needed)
try:
p = Popen(cmd, stdout=PIPE, \
stderr=PIPE, shell=True, env=env_home)
while p.poll() is None:
gevent.sleep(0.1)
now = datetime.datetime.now()
diff = now - times
if diff.seconds > 5:
os.kill(p.pid, signal.SIGKILL)
os.waitpid(-1, os.WNOHANG)
message = "command:" + cmd + " ---> hanged"
ssdlog = StorageStatsDaemonLog(message = message)
self.call_send(ssdlog)
return None
except:
pass
return None
# stdout is used
return p.stdout.read()
示例2: copper_node
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
def copper_node(workdir):
logpath = os.path.join(workdir, 'copper.log')
unixpath = os.path.join(workdir, 'copper.sock')
httppath = os.path.join(workdir, 'copper.http')
confpath = os.path.join(workdir, 'copper.conf')
config = {
"listen": [
{
"net": "unix",
"type": "http",
"addr": httppath,
},
{
"net": "unix",
"addr": unixpath,
"allow-changes": True,
},
],
}
with open(confpath, 'w') as f:
# YAML parses valid JSON data
json.dump(config, f)
with open(logpath, 'wb') as logfile:
p = Popen(['copper-node', '-config=' + confpath], shell=False, cwd=workdir, stdout=logfile, stderr=logfile)
try:
while not os.path.exists(unixpath):
time.sleep(0.001)
rc = p.poll()
if rc is not None:
with open(logpath, 'rb') as logfile:
sys.stderr.write(logfile.read())
raise RuntimeError('copper-node exited with status %r' % (rc,))
yield {
'unix': unixpath,
'http': httppath,
}
finally:
if p.poll() is None:
p.terminate()
p.wait()
示例3: proc
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
def proc(input, output, args, env=None, stderr=None):
"""Run a subprocess, passing input to its stdin and sending its stdout to output,
with each item newline-seperated.
Args is either a string to be shell-interpreted, or a list of args.
stderr is either not redirected (default), mixed in with stdout (pass subprocess.STDOUT),
or redirected to a given file.
"""
from gevent.subprocess import Popen, PIPE, STDOUT
if isinstance(args, unicode):
args = args.encode('utf8')
if isinstance(args, str):
shell = True
else:
shell = False
group = gevent.pool.Group()
proc = None
try:
proc = Popen(args, shell=shell, env=env, stdin=PIPE, stdout=PIPE, stderr=stderr)
@group.spawn
def do_input():
for item in input:
item = item.encode('utf8') if isinstance(item, unicode) else str(item)
proc.stdin.write('{}\n'.format(item))
proc.stdin.flush()
proc.stdin.close()
@group.spawn
def do_output():
for line in proc.stdout:
output.put(line.rstrip('\n'))
output.close()
proc.wait()
group.join()
finally:
if proc and proc.poll() is None:
try:
proc.kill()
except OSError as e:
if e.errno != errno.ESRCH:
raise
示例4: alarm
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
import sys
from gevent.subprocess import Popen
from util import alarm
alarm(3)
popen = Popen([sys.executable, '-c', 'pass'])
while popen.poll() is None:
pass
示例5: __init__
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
#.........这里部分代码省略.........
with closing(open(pconfig, 'wb')) as cfg:
cfg.write(PLATFORM_CONFIG_UNRESTRICTED.format(**config))
parser.write(cfg)
elif self.mode == RESTRICTED:
if not RESTRICTED_AVAILABLE:
raise ValueError("restricted is not available.")
certsdir = os.path.join(self.volttron_home, 'certificates')
print ("certsdir", certsdir)
self.certsobj = certs.Certs(certsdir)
with closing(open(pconfig, 'wb')) as cfg:
cfg.write(PLATFORM_CONFIG_RESTRICTED.format(**config))
else:
raise PlatformWrapperError(
"Invalid platform mode specified: {}".format(mode))
log = os.path.join(self.volttron_home, 'volttron.log')
if enable_logging:
cmd = ['volttron', '-vv', '-l{}'.format(log)]
else:
cmd = ['volttron', '-l{}'.format(log)]
print('process environment: {}'.format(self.env))
print('popen params: {}'.format(cmd))
self.p_process = Popen(cmd, env=self.env, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
assert self.p_process is not None
# A None value means that the process is still running.
# A negative means that the process exited with an error.
assert self.p_process.poll() is None
self.serverkey = self.keystore.public
assert self.serverkey
agent = self.build_agent()
has_control = False
times = 0
while not has_control and times < 10:
times += 1
try:
has_control = agent.vip.peerlist().get(timeout=.2)
except gevent.Timeout:
pass
if not has_control:
self.shutdown_platform()
raise "Couldn't connect to core platform!"
if bind_web_address:
times = 0
has_discovery = False
while times < 10:
times += 1
try:
resp = requests.get(self.discovery_address)
if resp.ok:
has_discovery = True
break
except Exception as e:
gevent.sleep(0.1)
self.logit("Connection error found {}".format(e))
if not has_discovery:
示例6: run
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
#.........这里部分代码省略.........
err_to_out (bool): redirect stderr to stdout if True, default is False
input (str or tuple of str): str will be flushed to stdin after executed command, default is None
force (bool): executing full operations even if envs.common.dry_run is True
**kwargs (dict): add only for supporting dry-run replacing
Return:
str if freturn is False: string that contained all stdout messages
tuple if freturn is True:
string that contained all stdout messages
string that contained all stderr
int that mean return code of command
"""
# hack for dry-run
if envs.common.dry_run and not force:
from dry_operations import run
return run(command, use_sudo, user, group, freturn, err_to_out, input, **kwargs)
logger = envs.connect.logger
interactive = envs.common.interactive
parallel = envs.common.parallel
host_string = ''.join((envs.connect.user,
'@',
envs.connect.host))
logger.debug('executing run function')
logger.debug('arguments for executing and another locals: %s', locals())
command = command_patching_for_sudo(command, use_sudo, user, group)
# logging
write_message_to_log(command, 'in: ')
stderr = PIPE
if err_to_out:
stderr = STDOUT
logger.debug('stderr: %s', stderr)
# open new connect
if envs.connect.host in envs.common.localhost:
logger.debug('executing command %s with shell=True', command)
p = Popen(command, stdout=PIPE, stderr=stderr, stdin=PIPE, shell=True)
else:
scommand = [
envs.common.ssh_binary,
envs.common.ssh_port_option,
str(envs.connect.port),
host_string,
]
scommand += envs.common.ssh_args.split()
scommand += envs.connect.con_args.split()
scommand += [command]
logger.debug('executing command %s', scommand)
p = Popen(scommand, stdout=PIPE, stderr=stderr, stdin=PIPE)
# flush input
if input:
if type(input) is str:
input = [input]
for s in input:
s = str(s)
if s[-1] not in ('\n', '\r'):
s += '\n'
logger.debug('flushing input %s', s)
p.stdin.write(s)
p.stdin.flush()
# run another command
if parallel:
gevent.sleep(0)
logger.debug('run another command with gevent.sleep(0)')
# processing std loop
threads = []
if interactive:
args = (p, copy(envs.common), copy(envs.connect))
gin = gevent.spawn(in_loop, *args)
logger.debug('executing in_loop with args %s', args)
threads.append(gin)
args = (p, copy(envs.common), copy(envs.connect))
gout = gevent.spawn(out_loop, *args)
logger.debug('executing out_loop with args %s', args)
threads.append(gout)
if not err_to_out:
args = (p, copy(envs.common), copy(envs.connect), True)
gerr = gevent.spawn(out_loop, *args)
logger.debug('executing err_loop with args %s', args)
threads.append(gerr)
gevent.joinall(threads)
logger.debug('child process has terminated with status %s', p.returncode)
#TODO: check returncode if returncode==None
sumout = gout.value
sumerr = gerr.value if not err_to_out else ''
status = p.returncode
if p.poll() is None:
p.terminate()
p.kill()
if freturn:
logger.debug('return sumout %s, sumerr %s, status %s', sumout, sumerr, status)
return (sumout, sumerr, status)
logger.debug('return sumout %s', sumout)
return sumout
示例7: __init__
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
#.........这里部分代码省略.........
if not RESTRICTED_AVAILABLE:
raise ValueError("restricted is not available.")
certsdir = os.path.join(os.path.expanduser(self.env['VOLTTRON_HOME']),
'certificates')
print ("certsdir", certsdir)
self.certsobj = certs.Certs(certsdir)
with closing(open(pconfig, 'wb')) as cfg:
cfg.write(PLATFORM_CONFIG_RESTRICTED.format(**config))
opts = type('Options', (), {'resource-monitor':False,
'verify_agents': True,
'volttron_home': self.volttron_home})()
else:
raise PlatformWrapperError("Invalid platform mode specified: {}".format(mode))
log = os.path.join(self.env['VOLTTRON_HOME'], 'volttron.log')
if enable_logging:
cmd = ['volttron', '-vv', '-l{}'.format(log)]
else:
cmd = ['volttron', '-l{}'.format(log)]
if self.opts['developer_mode']:
cmd.append('--developer-mode')
self._p_process = Popen(cmd, env=self.env, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
assert self._p_process is not None
# A None value means that the process is still running.
# A negative means that the process exited with an error.
assert self._p_process.poll() is None
# # make sure we don't return too quickly.
gevent.sleep(0.2)
#os.environ['VOLTTRON_HOME'] = self.opts['volttron_home']
#self._p_process = Process(target=start_volttron_process, args=(self.opts,))
#self._p_process.daemon = True
#self._p_process.start()
gevent.sleep(0.2)
self.use_twistd = use_twistd
#TODO: Revise this to start twistd with platform.
if self.use_twistd:
tconfig = os.path.join(self.volttron_home, TMP_SMAP_CONFIG_FILENAME)
with closing(open(tconfig, 'w')) as cfg:
cfg.write(TWISTED_CONFIG.format(**config))
tparams = [TWISTED_START, "-n", "smap", tconfig]
self._t_process = subprocess.Popen(tparams, env=self.env)
time.sleep(5)
#self._t_process = subprocess.Popen(["twistd", "-n", "smap", "test-smap.ini"])
def is_running(self):
self.logit("PROCESS IS RUNNING: {}".format(self._p_process))
return self._p_process is not None and self._p_process.poll() is None
def twistd_is_running(self):
return self._t_process is not None
# def publish(self, topic, data):
示例8: Command
# 需要导入模块: from gevent.subprocess import Popen [as 别名]
# 或者: from gevent.subprocess.Popen import poll [as 别名]
class Command(object):
def __init__(self, name, command, **kwargs):
self.name = name
self.command = command
self.log = None
self.stop = STOPPED #0 normal stopped 1 running -1 fatal stopped
self.process = None
for k, v in kwargs.items():
setattr(self, k, v)
self.start_time = None
self.stop_time = None
def is_ok(self):
return self.process and self.process.poll() is None
def do_start(self):
if not self.log:
self.log = RotatingFile(self.logfile,
maxBytes=self.logfile_maxbytes,
backupCount=self.logfile_backups)
if self.is_ok():
msg = self.name + ' has already been started'
else:
self.log.info('start '+self.name)
n = 0
while n < self.startretries:
self.process = Popen(self.command, stdout=self.log, stderr=self.log, shell=True, cwd=self.cwd)
gevent.sleep(self.starting_time)
if self.process.poll() is not None:
#error
self.log.info('start '+self.name+' failed!, times=%d'%n)
n += 1
else:
self.start_time = datetime.datetime.now()
break
#if stopped then status is Fatal
if n == self.startretries:
self.stop = FATAL
msg = self.name + ' started failed'
else:
self.stop = RUNNING
msg = self.name + ' started'
return msg
def do_stop(self):
if self.is_ok():
self.log.info('stop '+self.name)
self.stop = STOPPED
self.stop_time = datetime.datetime.now()
call(['taskkill', '/F', '/T', '/PID', str(self.process.pid)])
self.process = None
msg = self.name + ' stopped'
else:
msg = self.name + ' has already stopped'
return msg
def do_status(self):
if self.is_ok():
status = 'RUNNING'
info = 'pid %d, uptime %s' % (self.process.pid, self.start_time.strftime('%H:%M:%S'))
else:
if self.stop == STOPPED:
status = 'STOPPED'
info = self.stop_time.ctime()
else:
status = 'FATAL'
info = 'Exited too quickly'
msg = '%-20s %-10s %s' % (self.name, status, info)
return msg