4

I am learning multithreading and thinking about using it with Arcpy. The example I am working on doesn't have much practical reasons. However, I don't know why it fails on the line "arcpy.Exists(fcPath):" even though the SDE is running and the feature class in question is accessible in ArcCatalog. It didn't help if I used env.workspace.


import threading
import arcpy
from arcpy import env

def tf(conn_string):
    global tTotal
    #env.workspace = conn_string
    fcName = r"GIS_USER.TEST_FC"
    fcPath = conn_string + "\\" + fcName
    if arcpy.Exists(fcPath):
        threadLock.acquire()
        tTotal += 1
        threadLock.release()       
    else:
        print('%d: failed to connect to %s'%(tTotal, conn_string))

def mt(conn_string, step): 
    global tTotal 
    thread_list = []

    for num in range(step):
        t = threading.Thread(target=tf, args=(conn_string,))
        thread_list.append(t)
        t.start()

    for t in thread_list: 
        t.join()

    print "total = %d"%tTotal    

tTotal = 0
conn_string = r"C:\test_conn.sde"
threadLock = threading.Lock()
mt(conn_string, 5)

0

1 Answer 1

8

This just plain won't work, you're going to need to look into multiprocessing if you want parallelism. ArcGIS Desktop/Server's architecture is based on the COM Single-Threaded Apartment model, meaning that any objects created on one thread can only be used on that thread. You can create a new ArcObjects object graph on another thread (see the linked documentation), at which point you'd need to use comtypes on the other thread(s) and use ArcObjects via the API generated by comtypes. This isn't for the faint of heart.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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