2

the following is a line from a python program that calls the "demo.exe" file. a window for demo.exe opens when it is called, is there any way for demo.exe to run in the "background"? that is, i don't want the window for it show, i just want demo.exe to run.


p = subprocess.Popen(args = "demo.exe", stdout = subprocess.PIPE)

the output of demo.exe is used by the python program in real time, so demo.exe is not something that i can have run in advance of running the python program. demo.exe handles a lot of on the fly back-end calculations. i'm using windows xp.

thanks in advance!

B Rivera
  • 361
  • 1
  • 5
  • 15

2 Answers2

4

Thanks to another StackOverflow thread, I think this is what you need:

startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(args = "demo.exe", stdout=subprocess.PIPE, startupinfo=startupinfo)

I tested on my Python 2.6 on XP and it does indeed hide the window.

Community
  • 1
  • 1
ewall
  • 27,179
  • 15
  • 70
  • 84
  • 1
    If you set `STARTF_SHOWWINDOW`, you will also want to initialise the `wShowWindow` member of `startupinfo` to one of the `SW_*` constants. This method relies on the program that you run actually acting upon the `wShowWindow` flag; it's not required to do so. – Greg Hewgill Sep 01 '09 at 01:39
3

Try this:

from subprocess import Popen, PIPE, STARTUPINFO, STARTF_USESHOWWINDOW
startupinfo = STARTUPINFO()
startupinfo.dwFlags |= STARTF_USESHOWWINDOW
p = Popen(cmdlist, startupinfo=startupinfo, ...)
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • If you set `STARTF_SHOWWINDOW`, you will also want to initialise the `wShowWindow` member of `startupinfo`. This method relies on the program that you run actually acting upon the `wShowWindow` flag; it's not required to do so. – Greg Hewgill Sep 01 '09 at 00:55
  • i upvoted both answers and flipped a coin for best answer, ewall won. – B Rivera Sep 01 '09 at 01:18
  • 2
    Ah well. I got my answer in first (by 8 minutes, no less!) and I still don't get the points. :-) No worries, ewall needs the upvotes more than I do. – Daniel Pryden Sep 01 '09 at 18:04
  • ah, sorry, i didn't pay attention to time. still new here. – B Rivera Sep 02 '09 at 03:46