How to do os.execv() in Python in Windows without detaching from the console?

2024/11/17 5:52:56

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)

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)

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

Terminate batch job (Y/N)?

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.

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

Answer

you can use subprocess.call method like this example

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

see this link for more information about subprocess class:

subprocess — Subprocess management

https://en.xdnf.cn/q/71259.html

Related Q&A

Django queryset permissions

I am building a quite complex Django application to be used on top of and email scanning service. The Django application is written using Python 3.5+This application primarily uses Django Rest Framewor…

How to extract certain parts of a web page in Python

Target web page: http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htmThe section I want to extract:<tr><td>Skilled &ndash; Independent (Residence) sub…

Accessing axis or figure in python ggplot

python ggplot is great, but still new, and I find the need to fallback on traditional matplotlib techniques to modify my plots. But Im not sure how to either pass an axis instance to ggplot, or get one…

Importing the same modules in different files

Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should t…

Manually calculate AUC

How can I obtain the AUC value having fpr and tpr? Fpr and tpr are just 2 floats obtained from these formulas:my_fpr = fp / (fp + tn) my_tpr = tp / (tp + fn) my_roc_auc = auc(my_fpr, my_tpr)I know thi…

Plotly: How to make stacked bar chart from single trace?

Is it possible to have two stacked bar charts side by side each of them coming from a single column?Heres my df:Field IssuePolice Budget cuts Research Budget cuts Police Time consum…

zip function help with tuples

I am hoping someone can help me with a problem Im stuck with. I have a large number of tuples (>500) that look like this:(2,1,3,6) (1,2,5,5) (3,0,1,6) (10,1,1,4) (0,3,3,0) A snippet of my c…

Mini batch training for inputs of variable sizes

I have a list of LongTensors, and another list of labels. Im new to PyTorch and RNNs so Im quite confused as to how to implement minibatch training for the data I have. There is much more to this data,…

Python gTTS, is there a way to change the speed of the speech

It seems that on gTTS there is no option for changing the speech of the text-to-speech apart from the slow argument. I would like to speed up the sound by 5%. Any suggestion on how I can do it? Best.t…

Change QLabel text dynamically in PyQt4

My question is: how can I change the text in a label? The label is inside a layout, but setText() does not seem to work - maybe I am not doing it right. Here is my code:this is the Main windows GUI, t…