Skip to main content
4 of 7
deleted 1516 characters in body
kflaw
  • 1.4k
  • 17
  • 43

How to write output from subprocess to disk (using multiprocessing)

This code was developed from another question I posted about how to kill an arcpy tool if it takes longer than a specified time. See How to cancel tool execution in python. Using the multiprocssing module as a subprocess of the function seems to work, the only problem is i can't see how to write the output to disk. The function specifies a path to write the data to, and when I run the function without the subprocess, I can see the files on disk. But when the subprocess is included it doesn't look the files are being written out to disk. Can anyone help?

import os, arcpy, arcgisscripting, time, sys
gp = arcgisscripting.create()

def ConvertCADtoGDB(in_dgn,out_gdb):
    gp.ImportCAD_conversion(in_dgn,out_gdb,'','Explode_Complex')
    if not arcpy.Exists(out_gdb):raise RuntimeError('%s does not exist'%out_gdb)

def main(srch_dir,gdb_dir,timeout):
    for dirpath, dirnames, files in os.walk(srch_dir):
        for f in files:
            f_name, f_ext = os.path.splitext(f)
            in_dgn = os.path.join(dirpath,f)
            out_gdb = os.path.join(gdb_dir,f_name+'.gdb')
            if f_ext == '.dgn':
                if not os.path.isdir(out_gdb):
                    print 'creating',out_gdb
                    #####################################################
                    # Create a separate process to run the tool in
                    p = multiprocessing.Process(target=ConvertCADtoGDB,
                                            args=(in_dgn,out_gdb))
                    p.start()
                    p.join(timeout)   # Wait for process to complete
                    if p.is_alive():  # Terminate process if it is 
                        p.terminate() # still running after the timeout
                        print('Conversion of %s terminated'%in_dgn)
                    else:
                        print('Conversion of %s successful'%in_dgn)
                    #####################################################

if __name__ == '__main__':
    srch_dir=r'C:\Temp\dgns'
    gdb_dir=r'C:\Temp\gdbs'
    timeout=5 #Seconds
    main(srch_dir,gdb_dir,timeout)
kflaw
  • 1.4k
  • 17
  • 43