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 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. Any thought or suggestion would be appreciated.


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)

share|improve this question
    
How do you know it fails, what happens? Does it work if you take out all the multithreading stuff? –  blah238 Apr 2 at 21:47
add comment

1 Answer 1

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.

share|improve this answer
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.