By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

Get python to continue if process hangs

P: 1
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
Expand|Select|Wrap|Line Numbers
  1. import sys, string, os, arcgisscripting, shutil
  2.  
  3. # Create the Geoprocessor object
  4. gp = arcgisscripting.create()
  5.  
  6. WCH_gdb = "\\\\hgis01\\gishome\\ladietz\\WCH.gdb"
  7.  
  8. gp.Compact_management(WCH_gdb)
  9.  
  10.  
  11.  
Dec 17 '10 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 624
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.
Expand|Select|Wrap|Line Numbers
  1. import time
  2. from multiprocessing import Process
  3.  
  4. class TestClass():
  5.  
  6.    def test_f(self, name):
  7.       ctr = 0
  8.       while True:
  9.          ctr += 1
  10.          print ctr, name
  11.          time.sleep(0.5)
  12.  
  13. if __name__ == '__main__':
  14.    CT=TestClass()
  15.    p = Process(target=CT.test_f, args=('P',))
  16.    p.start()
  17.  
  18.    ## sleep for 5 seconds and terminate
  19.    time.sleep(5.0)
  20.    p.terminate()
  21.    p.join() 
Dec 17 '10 #2

Expert 100+
P: 624
You can also use subprocess if running a separate program file. It would be something along these lines.
Expand|Select|Wrap|Line Numbers
  1.       proc = subprocess.Popen(program_to_run) ## may or may not require shell=True
  2.       time.sleep(sleep_secs)
  3.       os.kill(proc.pid, signal.SIGUSR1) 
Dec 17 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.