Exiting os.spawnv's subroutine

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IamIan

    #1

    Exiting os.spawnv's subroutine

    I am using os.spawnv in Python 2.1 to do some geoprocessing in a
    subroutine/process. Everything works great, except when the processing
    is done the subroutine just waits for a couple minutes before closing
    itself and returning to the main script. I have tried using sys.exit()
    and exit() but these aren't doing anything.

    What is the proper way to terminate this subroutine upon completion,
    rather than waiting? Thank you.

  • Donn Cave

    #2
    Re: Exiting os.spawnv's subroutine

    In article <1140215299.383 562.57250@g14g2 000cwa.googlegr oups.com>,
    "IamIan" <iansan@gmail.c om> wrote:
    [color=blue]
    > I am using os.spawnv in Python 2.1 to do some geoprocessing in a
    > subroutine/process. Everything works great, except when the processing
    > is done the subroutine just waits for a couple minutes before closing
    > itself and returning to the main script. I have tried using sys.exit()
    > and exit() but these aren't doing anything.
    >
    > What is the proper way to terminate this subroutine upon completion,
    > rather than waiting? Thank you.[/color]

    Your description is a little ambiguous, but my guess is your
    process just isn't done when you think it is. It's flushing
    data to disk or something like that. Or it could be something
    else. Why don't you write a sample program that works like
    this, and demonstrates the problem, and then we'll know.

    Donn Cave, donn@u.washingt on.edu

    Comment

    • IamIan

      #3
      Re: Exiting os.spawnv's subroutine

      My code is below. As a single script there is no pause at the end of
      the processing as there is with using os.spawnv... I am using the
      P_WAIT value, and wonder if it is responsible for the extra time at the
      end of each iteration. Could it take longer for the processing to be
      "successful " when run under os.spawnv? Is there a way to manually send
      a "success" exit code back to os.spawnv, rather than waiting for it?
      Thanks.

      Main script:
      # Import subprocess modules
      import os, sys, win32com.client

      # Define arguments for subprocess
      pyPath = "C:\\Python21\\ python.exe"
      contourScript = "C:\\Ian\\Pytho n scripts\\subPro cess2.py"
      workspace = "D:\\GIS\\T est"

      # Get a list of IMG files in the workspace for Contouring
      filenames = os.listdir(work space)
      filenames = [filename.lower( )
      for filename in filenames
      if (filename[-4:].lower() == ".img" and filename[0:2] != "h_" )]
      for filename in filenames:

      # Define filenames
      print "Filename is " + filename
      inImg = workspace + "\\" + filename
      outContour50 = workspace + "\\contour5 0_" + filename[:-4] + ".shp"
      outContour100 = workspace + "\\contour1 00_" + filename[:-4] +
      ".shp"
      outContour200 = workspace + "\\contour2 00_" + filename[:-4] +
      ".shp"
      outContour500 = workspace + "\\contour5 00_" + filename[:-4] +
      ".shp"

      # Create parameter list
      parameterList = []

      # First parameter is the name of the Python executable
      parameterList.a ppend('python.e xe')

      # Second parameter is the full path of the Python script
      parameterList.a ppend(contourSc ript)

      # The following parameters are the arguments for the Batch script
      parameterList.a ppend(inImg)
      parameterList.a ppend(outContou r50)
      parameterList.a ppend(outContou r100)
      parameterList.a ppend(outContou r200)
      parameterList.a ppend(outContou r500)

      # Run subprocess
      os.spawnv(os.P_ WAIT, pyPath, parameterList)

      print "All done!"


      Secondary script:
      # Import system modules
      import sys, win32com.client

      # Create the geoprocessor object
      gp = win32com.client .Dispatch("esri Geoprocessing.G pDispatch.1")

      # Confirm license availability
      print "ArcInfo license is " + str(gp.CheckPro duct("ArcInfo") )
      gp.SetProduct(" ArcInfo")
      gp.CheckOutExte nsion("Spatial" )

      # Set arguments to be passed to main script
      inImg = sys.argv[1]
      outContour50 = sys.argv[2]
      outContour100 = sys.argv[3]
      outContour200 = sys.argv[4]
      outContour500 = sys.argv[5]

      badImgList = []

      try:
      # For each IMG file, contour at 50, 100, and 200 meter intervals
      gp.Contour_sa(i nImg, outContour50, "50", "0", "1")
      print "Successful ly contoured at 50 meter interval!"

      gp.Contour_sa(i nImg, outContour100, "100", "0", "1")
      print "Successful ly contoured at 100 meter interval!"

      gp.Contour_sa(i nImg, outContour200, "200", "0", "1")
      print "Successful ly contoured at 200 meter interval!"

      gp.Contour_sa(i nImg, outContour500, "500", "0", "1")
      print "Successful ly contoured at 500 meter interval!"
      except:
      badImgList.appe nd(filename)
      print filename + " is no good! It's been added to the list!"

      Comment

      • IamIan

        #4
        Re: Exiting os.spawnv's subroutine

        Strange but removing the try/except part of the second script (leaving
        only the processing) removed the 2 minute lag at the end of each
        subroutine.

        Comment

        Working...