Cross-platform subprocess with hidden window

I want to open a process in the background and interact with it, but this process should be invisible in both Linux and Windows. In Windows you have to do some stuff with STARTUPINFO, while this isn’t valid in Linux:

ValueError: startupinfo is only supported on Windows platforms

Is there a simpler way than creating a separate Popen command for each OS?

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    proc = subprocess.Popen(command, startupinfo=startupinfo)
if os.name == 'posix':
    proc = subprocess.Popen(command)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can reduce one line 🙂

startupinfo = None
if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
proc = subprocess.Popen(command, startupinfo=startupinfo)

Method 2

Just a note: for Python 2.7 I have to use subprocess._subprocess.STARTF_USESHOWWINDOW instead of subprocess.STARTF_USESHOWWINDOW.

Method 3

I’m not sure you can get much simpler than what you’ve done. You’re talking about optimising out maybe 5 lines of code. For the money I would just get on with my project and accept this as a consquence of cross-platform development. If you do it a lot then create a specialised class or function to encapsulate the logic and import it.

Method 4

You can turn your code into:

params = dict()

if os.name == 'nt':
    startupinfo = subprocess.STARTUPINFO()
    startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    params['startupinfo'] = startupinfo

proc = subprocess.Popen(command, **params)

but that’s not much better.

Method 5

I have tried the following solutions given on StackOverflow as well as on some other referral websites, but it does not work for me:

(1)

si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
checkProcess = subprocess.Popen(cmdline, stdout=subprocess.PIPE, 
startupinfo=si)
output = checkProcess.communicate()
(2)

checkProc = subprocess.Popen(cmdline, creationflags= subprocess.CREATE_NO_WINDOW,  stdout=subprocess.PIPE) # Also check the CREATE_NO_WINDOW = 0x08000000
output = checkProcess.communicate() 

(3)
checkProc = subprocess.Popen(cmdline,  stdout=subprocess.PIPE, shell =  False) #Also check the Shell = True
output = checkProcess.communicate()

(4)
startupinfo = None
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
checkProc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, startupinfo=startupinfo)
output = checkProcess.communicate()

(5)
checkProc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
output = checkProcess.communicate()

(6)
checkProc = subprocess.Popen(cmdline, stdout=subprocess.PIPE,stdin = subprocess.DevNull)
output = checkProcess.communicate()

Any Solution Please..?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x