Get python to continue if process hangs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arcazy
    New Member
    • Dec 2010
    • 1

    Get python to continue if process hangs

    Hi All,

    I am using python to script some processes using ESRI's ArcMap geoprocessor module.

    One of the utilities I have compacts my spatial database. When I run my script, sometimes it hangs on the compact call, and then the rest of the script doesn't complete. Other times it completes just fine but can't be trusted to finish.

    I don't think there is a way for python to check the status of the process to see if it has hung. So, was wondering if there is a way to tell python to move on to the next line in the script if it doesn't finish within say an hour.

    OR, is there a way to call the compact routines from a different python script, and have it return after a set amount of time?

    Anyway, pretty new to this python, but am learning. If anyone has any better suggestions, I'm open to them.

    Thanks in advance,

    R_

    here is an example snippet
    Code:
    import sys, string, os, arcgisscripting, shutil
    
    # Create the Geoprocessor object
    gp = arcgisscripting.create()
    
    WCH_gdb = "\\\\hgis01\\gishome\\ladietz\\WCH.gdb"
    
    gp.Compact_management(WCH_gdb)
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    I would use multiprocessing. You have 2 processes, the geoprocessor module and the timer, so you have to use a method that can handle multiple processes. The following is an example of a time out.
    Code:
    import time
    from multiprocessing import Process
    
    class TestClass():
    
       def test_f(self, name):
          ctr = 0
          while True:
             ctr += 1
             print ctr, name
             time.sleep(0.5)
    
    if __name__ == '__main__':
       CT=TestClass()
       p = Process(target=CT.test_f, args=('P',))
       p.start()
    
       ## sleep for 5 seconds and terminate
       time.sleep(5.0)
       p.terminate()
       p.join()

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      You can also use subprocess if running a separate program file. It would be something along these lines.
      Code:
            proc = subprocess.Popen(program_to_run) ## may or may not require shell=True
            time.sleep(sleep_secs)
            os.kill(proc.pid, signal.SIGUSR1)

      Comment

      Working...