Skip to main content
Question

Using subprocess.popen to call FME Workbenches

  • December 2, 2022
  • 3 replies
  • 111 views

jrlalessi
Contributor
Forum|alt.badge.img+6

Hi I am looking for information on how to appropriately use the FME Command in a python loop using subprocess.popen to call the argument. I am struggling to find info on how to achieve this. I am getting syntax warnings in my command when I use subproces.popen. But if I past directly to the cmd prompt I dont have issues. So Im missing something. Any help is appreciated. Please ignore all my comments :P

3 replies

jrlalessi
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • December 2, 2022

Oh no I posted without the screenshot

image


boydfme
Contributor
Forum|alt.badge.img+7
  • Contributor
  • December 9, 2022
import subprocess
fme_ = r"E:\Program Files\FME\fme.exe" ##fme.exe
_Workbench = r"your path to workbench\FME_option_poc.fmw"
 
arg1 = r"path to .gdb"
arg2 = ""
arg3 = r"your path to csv.csv"
arg4 = "Complete"
 
command = f'"{fme_}" "{_Workbench}" --SourceDataset_GEODATABASE_FILE "{arg1}" --False_Time "{arg2}" --DestDataset_CSV2 "{arg3}" --FME_Status_Entry "{arg4}"'
print (command)
starter = subprocess.STARTUPINFO()
starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
 
process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate() # executes command and gives you the standard out and standard error if you want.

 

Here is a python function example that also retrieves the standard out and standard error messages from the workbench - advanced.

def sub__fmeworker(arg1, arg2):
    """ command variable object houses the subprocess cmd string line.
    1. _workbench = fme workbench.fmw, 2. arg1, 3. arg2
 
    The subprocess.Popen() executes the command and retrieves the process outputs to stdout and stderr."""
    text = ''
 
    try:
        fme_ = r"C:\Program Files\FME\fme.exe" ##fme.exe
        command = f'"{fme_}" "{_Workbench}" --FEATURE_TYPES "{arg1}" --LogFile "{arg2}"'
        starter = subprocess.STARTUPINFO()
        starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
 
        process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stderr:
            stderr_de = str(stderr.decode("utf-8")) # Need to remove byte object
            msg = stderr_de.split("\r\n")[0]
            text += arg1 +'  '+ msg
        else:
            stdout_de = str(stdout.decode("utf-8")) # Need to remove byte object
            msg = stdout_de.split("\r\n")
            text += msg+'ERROR########'
    except:  #if the workbench fails add error tag
        text += arg1+'ERROR########'
    return text

 


jrlalessi
Contributor
Forum|alt.badge.img+6
  • Author
  • Contributor
  • December 12, 2022
boydfme wrote:
import subprocess
fme_ = r"E:\Program Files\FME\fme.exe" ##fme.exe
_Workbench = r"your path to workbench\FME_option_poc.fmw"
 
arg1 = r"path to .gdb"
arg2 = ""
arg3 = r"your path to csv.csv"
arg4 = "Complete"
 
command = f'"{fme_}" "{_Workbench}" --SourceDataset_GEODATABASE_FILE "{arg1}" --False_Time "{arg2}" --DestDataset_CSV2 "{arg3}" --FME_Status_Entry "{arg4}"'
print (command)
starter = subprocess.STARTUPINFO()
starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
 
process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate() # executes command and gives you the standard out and standard error if you want.

 

Here is a python function example that also retrieves the standard out and standard error messages from the workbench - advanced.

def sub__fmeworker(arg1, arg2):
    """ command variable object houses the subprocess cmd string line.
    1. _workbench = fme workbench.fmw, 2. arg1, 3. arg2
 
    The subprocess.Popen() executes the command and retrieves the process outputs to stdout and stderr."""
    text = ''
 
    try:
        fme_ = r"C:\Program Files\FME\fme.exe" ##fme.exe
        command = f'"{fme_}" "{_Workbench}" --FEATURE_TYPES "{arg1}" --LogFile "{arg2}"'
        starter = subprocess.STARTUPINFO()
        starter.dwFlags |= subprocess.STARTF_USESHOWWINDOW
 
        process = subprocess.Popen(command,startupinfo=starter, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        if stderr:
            stderr_de = str(stderr.decode("utf-8")) # Need to remove byte object
            msg = stderr_de.split("\r\n")[0]
            text += arg1 +'  '+ msg
        else:
            stdout_de = str(stdout.decode("utf-8")) # Need to remove byte object
            msg = stdout_de.split("\r\n")
            text += msg+'ERROR########'
    except:  #if the workbench fails add error tag
        text += arg1+'ERROR########'
    return text

 

Thank you! 

I found the error in my code and it was in the cmd. 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings