如何在Windows中使用Python在不脱离控制台的情况下执行os.execv()? [英] How to do os.execv() in Python in Windows without detaching from the console?

查看:61
本文介绍了如何在Windows中使用Python在不脱离控制台的情况下执行os.execv()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7上使用Python 2.6.我有Windows .cmd文件,该文件调用Python以运行CherryPy Web服务器(版本3.1.2).我通过在Windows CMD Shell中的提示符下执行该.cmd文件来启动它.当CherryPy注意到其依赖文件之一已更改时,它将重新启动.在Windows上,这种配置会引起一些问题,因为CherryPy用于重新启动自身的调用是

I'm using Python 2.6 on Windows 7. I have Windows .cmd file which invokes Python to run the CherryPy Web server (version 3.1.2). I start this .cmd file by executing it at the prompt in a Windows CMD shell. When CherryPy notices that one of its dependent files has changed, it restarts. There are a couple problems that arise in this configuration on Windows, because the invocation that CherryPy uses to restart itself is

os.execv(sys.executable, args)

,但是您无法在.cmd文件上调用Python可执行文件.我设法通过各种Python体操方法绕过了这个问题,现在我已经通过(基本上)调用来重新启动它.

but you can't invoke the Python executable on the .cmd file. I've managed to bypass that problem through various Python gymnastics, and now I've got it restarting by calling (essentially)

os.execv(sys.argv[0], args)

但是重新启动的过程与键盘分离;它会捕获Ctrl-C,然后提示我

But the restarted process is detached from the keyboard; it will catch Ctrl-C, and then I'm asked

Terminate batch job (Y/N)?

但是Y或N I类型不是由终止的.cmd文件捕获的,而是由Windows CMD Shell捕获的,并且它可以响应

but the Y or N I type is caught not by the terminating .cmd file, but by the Windows CMD shell, and it responds

'Y' is not recognized as an internal or external command, operable program or batch file.

那么我如何在Windows中进行设置,以使重新启动的进程保持在Unix中我称之为前台"的位置?

So how do I set things up in Windows to keep the restarted process in what I'd call "the foreground" in Unix?

推荐答案

您可以使用如本例所示的subprocess.call方法

you can use subprocess.call method like this example

import subprocess

retcode = subprocess.call(["ls", "-l"])

有关此子流程类的更多信息,请参见此链接:

see this link for more information about subprocess class:

子流程-子流程管理

这篇关于如何在Windows中使用Python在不脱离控制台的情况下执行os.execv()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆