我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用os.spawnle()。
def patch_new_process_functions_with_warning(): monkey_patch_os('execl', create_warn_multiproc) monkey_patch_os('execle', create_warn_multiproc) monkey_patch_os('execlp', create_warn_multiproc) monkey_patch_os('execlpe', create_warn_multiproc) monkey_patch_os('execv', create_warn_multiproc) monkey_patch_os('execve', create_warn_multiproc) monkey_patch_os('execvp', create_warn_multiproc) monkey_patch_os('execvpe', create_warn_multiproc) monkey_patch_os('spawnl', create_warn_multiproc) monkey_patch_os('spawnle', create_warn_multiproc) monkey_patch_os('spawnlp', create_warn_multiproc) monkey_patch_os('spawnlpe', create_warn_multiproc) monkey_patch_os('spawnv', create_warn_multiproc) monkey_patch_os('spawnve', create_warn_multiproc) monkey_patch_os('spawnvp', create_warn_multiproc) monkey_patch_os('spawnvpe', create_warn_multiproc) if sys.platform != 'win32': monkey_patch_os('fork', create_warn_multiproc) try: import _posixsubprocess monkey_patch_module(_posixsubprocess, 'fork_exec', create_warn_fork_exec) except ImportError: pass else: # Windows try: import _subprocess except ImportError: import _winapi as _subprocess monkey_patch_module(_subprocess, 'CreateProcess', create_CreateProcessWarnMultiproc)
def patch_new_process_functions(): # os.execl(path, arg0, arg1, ...) # os.execle(path, arg0, arg1, ..., env) # os.execlp(file, arg0, arg1, ...) # os.execlpe(file, arg0, arg1, ..., env) # os.execv(path, args) # os.execve(path, args, env) # os.execvp(file, args) # os.execvpe(file, args, env) monkey_patch_os('execl', create_execl) monkey_patch_os('execle', create_execl) monkey_patch_os('execlp', create_execl) monkey_patch_os('execlpe', create_execl) monkey_patch_os('execv', create_execv) monkey_patch_os('execve', create_execve) monkey_patch_os('execvp', create_execv) monkey_patch_os('execvpe', create_execve) # os.spawnl(mode, path, ...) # os.spawnle(mode, path, ..., env) # os.spawnlp(mode, file, ...) # os.spawnlpe(mode, file, ..., env) # os.spawnv(mode, path, args) # os.spawnve(mode, path, args, env) # os.spawnvp(mode, file, args) # os.spawnvpe(mode, file, args, env) monkey_patch_os('spawnl', create_spawnl) monkey_patch_os('spawnle', create_spawnl) monkey_patch_os('spawnlp', create_spawnl) monkey_patch_os('spawnlpe', create_spawnl) monkey_patch_os('spawnv', create_spawnv) monkey_patch_os('spawnve', create_spawnve) monkey_patch_os('spawnvp', create_spawnv) monkey_patch_os('spawnvpe', create_spawnve) if sys.platform != 'win32': monkey_patch_os('fork', create_fork) try: import _posixsubprocess monkey_patch_module(_posixsubprocess, 'fork_exec', create_fork_exec) except ImportError: pass else: # Windows try: import _subprocess except ImportError: import _winapi as _subprocess monkey_patch_module(_subprocess, 'CreateProcess', create_CreateProcess)
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()