Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a subprocess without having the system console open

Im trying to start a wxPython app that (I have converted to an exe with py2exe) from a process that is running in the background.

The problem is that when the gui app opens, so does a console window (c:\windows\system\cmd.exe)

I had a look at this question where Alex Martelli suggests setting the creationflags paramater of Popen to 0x08000000 but this hasn't solved my problem.

Also I wonder if there is a better way of running a process in the background, at the moment I just changed the extension of the script to pyw and since it doesn't have a GUI then it isn't visible...

This is line that calls the subprocess

    subprocess.Popen(args="%s"%comPort,bufsize=0,
                     executable="myFrozen_WxpythonApp.exe",
                     creationflags=0x08000000, shell=False)

py2exe script

...

options = {'py2exe': {'compressed': 3,
                          'optimize': 2,
                          'excludes': excludes,
                          'packages': packages,
                          'dll_excludes': dll_excludes,
                          'bundle_files': 1,
                          'dist_dir': 'dist',
                          'xref': False,
                          'skip_archive': False,
                          'ascii': False,
                          #'packages': packages,
                        'custom_boot_script': '',
                         }
                }  

      setup(options=options, windows=["app.pyw"], zipfile=None, data_files=data_files)

Update:

As I explained in my answer to this question the problem was in the subprocess.Popen call.

The first string in the args parameter should be the name of the executable, the executable name can then be followed by any commands or data that needs to be passed to the subprocess.

like image 277
volting Avatar asked Dec 04 '10 23:12

volting


People also ask

Does Popen need to be closed?

Popen do we need to close the connection or subprocess automatically closes the connection? Usually, the examples in the official documentation are complete. There the connection is not closed. So you do not need to close most probably.

What is difference between subprocess Popen and call?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

Is subprocess Popen a blocking call?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

Does subprocess call wait for completion?

The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code.


1 Answers

I figured out what I was doing wrong after reading the documentation for subprocess.Popen

The first string in the args parameter should be the name of the executable. I didn't include the name of the executable as I thought that was taken care of with the executable paramater.

like image 96
volting Avatar answered Sep 25 '22 06:09

volting