Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I attempted to spawn a separate process from the ArcGIS ToolValidator script. Unfortunately, it launches another instance of ArcCatalog.

  1. What is going on behind the scenes that causes ArcCatalog to be launched, when I'm only trying to execute a second python script in a separate process and get the results?

  2. How can I launch a separate process, wait for it to end, and get the results (standard out)?

Don't worry, I'm not trying to do heavy lifting from inside the tool dialog. I was just trying to bypass the background processing window when calling GetCount_management. That problem was solved, but I'm still curious why ArcCatalog gets launched.

share|improve this question
    
What syntax and parameters are you using to spawn the subprocess? i.e. are you using sys.executable to try and get the path to the python executable (which won't work when running from ArcCatalog)? Can you post a simple example? –  Luke Nov 24 '11 at 0:01
    
I used subprocess.call with a path to an existing python script. I just did some tests I tossed, I'll set it up again if you have suggestions to try. –  majgis Nov 24 '11 at 0:37
add comment

2 Answers

The following works for me in a ToolValidator class:

import sys,subprocess,os.path
subprocess.call([os.path.join(sys.exec_prefix,'pythonw'),path_to_script, script_arg_1,...,script_arg_N])
share|improve this answer
add comment

If you are looking to execute a piece of code rather than a whole script, it might be worth your while to look at the Multiprocessing module.

It will create a new process with separate resources, though still allowing you to share information between processes, if you so choose. The typical frame for this would be:

import multiprocessing as _mp

def _target(var1, var2):
    import arcpy #(if required) 
    #do work here
    pass

def main():
    process = _mp.Process(target=target, kwargs=(var1='data', var2='data')
    try:

        process.start()
        print process.pid
    except Exception, ex:
        print ex
    finally:
        process.terminate()
        process.join()

if you would like to transfer data between the processes, check out the python multiprocessing pages

share|improve this answer
1  
Just to add, I have no idea why ArcCatalog would need to spawn a second instance. I guess its just a feature! –  OptimizePrime Nov 24 '11 at 20:52
    
Thanks. I started with the multiprocessing module, and it also launches an instance of ArcCatalog. I'll get my tests set up again and respond back... –  majgis Nov 28 '11 at 20:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.