Select to view content in your preferred language

While using ArcGis Python toolbox I have a subprocess.popen.. how to stop command prompt screen from appearing while it runs?

4008
9
07-08-2016 07:27 AM
PaulHacker2
Deactivated User


I run a batch at the end of the .pyt Python Toolbox script so to do most of the process (it runs a .bat file that runs a python script.)

Yes a Python script runs a bat runs a Python script.

But when using subprocess.Popen("C:/output/BatchRun.bat", shell=False)

The command prompt window opens and I guess stays open till the bat is finished. The .pyt goes ahead and ends.

How to get rid of the command prompt window from opening?

Thanks!

0 Kudos
9 Replies
ShaunWalbridge
Esri Regular Contributor

Could you describe what you're trying to do? In most contexts, it should be possible to use Python directly to invoke the related Python script instead of needing the batch file middleman.

Luke_Pinner
MVP Regular Contributor

I agree with Shaun Walbridge​ - you should be able to run the python code directly. More info please.

However, you can do something like the following:

def runcmd(cmd):
    """
    A generic subprocess.Popen function to run a command which suppresses consoles on Windows
    Luke Pinner 2013
    """
    if os.name=='nt':
        #Windows starts up a console when a subprocess is run from a non-console
        #app like pythonw unless we pass it a flag that says not to...
        startupinfo=subprocess.STARTUPINFO()
        startupinfo.dwFlags |= 1            
    else:startupinfo=None
    proc=subprocess.Popen(cmd, startupinfo=startupinfo,
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                          stdin=subprocess.PIPE)
    if os.name=='nt':proc.stdin.close()
    stdout,stderr=proc.communicate()
    exit_code=proc.wait()
    return exit_code, stdout, stderr
PaulHacker2
Deactivated User

I had a similar setup but I was hoping it would NOT 'wait'... and thus show ArcGIS as being busy.

Even taking the proc.wait out does not help. Looking for a true background method to allow users to keep using ArcMAP.

Thanks.

0 Kudos
ShaunWalbridge
Esri Regular Contributor

Can you use background geoprocessing? It's designed for this purpose, and if you install 64-bit background geoprocessing, will give you the advantage of providing a 64-bit environment (e.g. access to more memory, some performance improvements on a 64-bit machine).

JoshuaBixby
MVP Esteemed Contributor

It has been a while since I worked with subprocess.  There are a couple of aspects to runcmd() that may be blocking, i.e., causing the wait.  If I can find some more time to investigate, I will offer a more specific suggestion if you want to address the blocking.


In the past, I have used some code that is similar to what is posted here, but I originally adapted it from Launching a subprocess without a console window (Python recipe).

import subprocess

def launchWithoutConsole(command, args=None):
    SI = subprocess.STARTUPINFO()
    SI.dwFlags |= subprocess.STARTF_USESHOWWINDOW
    return subprocess.Popen([command] + [a for a in args or []],
                            startupinfo = SI)

Part of this recipe relies on passing back the subprocess.Popen object instead of relying on it to finish within the function.  If I want to call it and forget or ignore any feedback, errors, etc...; I call the function and don't assign the results to any variable.

All of this said, I think calling a batch file to call a Python script is not a good workflow for the OP.

PaulHacker2
Deactivated User

Looking into it. Thanks Shaun. It looks real interesting!

0 Kudos
PaulHacker2
Deactivated User

Well I looked, we have no license for background geoprocessing. But can a Python script that was assembled and put into a regular .py file be submitted for nightly run somehow?

Thanks.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Background Geoprocessing, either 32-bit or 64-bit, is not a separate license.  32-bit background geoprocessing is built into the application.  64-bit Background Geoprocessing is a separate install, but it isn't a separate license.

BruceHarold
Esri Regular Contributor

Hi

The executable pythonw.exe exists for the purpose of not opening a command prompt for a subprocess.

I did a tech workshop at UC where I used this pattern, not in a PYT but it should be adaptable - attached.