arcpy.Describe()函数在新线程中报错怎么解决?

开发的项目中需要在新线程中判断一条路径的工作空间类型为文件夹还是gdb数据库,但是arcpy.Describe()函数在新线程中报错。特来求助!代码示例如下:

# -*- coding: utf-8 -*-
import arcpy
import threading

wkpath = "F:/Desktop/data.gdb"

def pathtype():
    desc = arcpy.Describe(wkpath)
    print("工作目录类型为:" + desc.workspaceType)

def thread_it(func, *args):
    t = threading.Thread(target = func, args = args) 
    t.setDaemon(False)
    t.start()

thread_it(pathtype)

这个目录是真实存在的,但报错说这个目录不存在:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.7\lib\threading.py", line 801, in __bootstrap_inner
 Custom Domain by Bitly()
  File "C:\Python27\ArcGIS10.7\lib\threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "F:\Desktop\test2.py", line 9, in pathtype
    desc = arcpy.Describe(wkpath)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\__init__.py", line 1260, in Describe
    return gp.describe(value, data_type)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\geoprocessing\_base.py", line 376, in describe
    self._gp.Describe(*gp_fixargs(args, True)))
IOError: "F:/Desktop/data.gdb" does not exist

改成 \ 试试

wkpath = r"F:\Desktop\data.gdb"


今天突发奇想,终于找到了问题的解决方式。那就是将“import arcpy”放到函数中。

# -*- coding: utf-8 -*-

import threading
wkpath = u"F:/Desktop/data.gdb"
print(u"工作目录为:" + wkpath)

def pathtype():
  import arcpy
  desc = arcpy.Describe(wkpath)
  print(u"工作目录类型为:" + desc.workspaceType)

def thread_it(func, *args):
  t = threading.Thread(target = func, args = args) 
  t.setDaemon(False)
  t.start()

thread_it(pathtype)


运行结果如下:

工作目录为:F:/Desktop/data.gdb
工作目录类型为:LocalDatabase
改成 \ 试试
wkpath = r"F:\Desktop\data.gdb"

没有作用,不是这个问题