Question

Using subprocess.popen to call FME Workbenches

  • 2 December 2022
  • 3 replies
  • 27 views

Badge +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

Badge +6

Oh no I posted without the screenshot

image

Badge
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

 

Badge +6
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. 

Reply