adamkerz

Bug with arcpy in a thread

Discussion created by adamkerz on Jul 29, 2012
Latest reply on Jul 30, 2012 by JScheirer-esristaff
I am trying to use arcpy within a thread and am getting unexpected results from arcpy.Exists:


import threading
import arcpy


class ThreadTest(threading.Thread):
    def __init__(self,):
        threading.Thread.__init__(self)

    def run(self):
        if arcpy.Exists(r'E:\nothing.gdb'):
            arcpy.Delete_management(r'E:\nothing.gdb')


if __name__=='__main__':
    tt = ThreadTest()
    tt.start()


This results in arcpy.Exists returning True when the .gdb does not exist (or anything even close) and then an error when trying to delete it as it does not in fact exist.

If I move the import arcpy line from the top to the run method, the problem disappears, however this is not a solution as it seems to only work for the thread that arcpy is initially imported in. What if I need a second thread?

When I put the if exists test under the if __name__==... line (and therefore in the thread where arcpy is initially imported) all works fine.

For some background, I'm trying to use this with a GUI so I only need one thread, but I need the processing to happen in that thread whilst the main thread continues to serve GUI events.

Am I doing something wrong, or is this a bug?

Cheers,

Adam

Outcomes