编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

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

Neha0595 Python 2022-5-11 10:57 7人围观

腾讯云服务器
如何在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()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^