如果失去与互联网的连接,则在执行功能时挂起

时间:2019-04-21 17:29:26

标签: python arcgis

from arcgis.gis import GIS
from IPython.display import display
gis = GIS("portal url", 'username', 'password')

#search for the feature layer named Ports along west coast
search_result = gis.content.search('title:Ports along west coast')

#access the item's feature layers
ports_item = search_result[0]
ports_layers = ports_item.layers

#query all the features and display it on a map
ports_fset = ports_layers[0].query() #an empty query string will return all 
ports_flayer = ports_layers[0]
ports_features = ports_fset.features

# select San Francisco feature
sfo_feature = [f for f in ports_features if f.attributes['port_name']=='SAN FRANCISCO'][0]
sfo_feature.attributes
try:
    update_result = ports_flayer.edit_features(updates=[sfo_edit])
except:
    pass

这是我展示的示例,其中我尝试更新要素图层。实际上,我正在循环更新记录,因此有很多记录。问题是在“ Internet连接”被击落的情况下,它卡在了edit_features函数中。

因此,除了继续该流程外,它不可能前进。

我只需要ctrl + c来停止脚本执行,因为它已被挂起并且使用了edit_features()函数。我该怎么办?

1 个答案:

答案 0 :(得分:2)

如果您遇到这种情况,我会搜索arcgis API文档以设置连接超时,如果找不到,我建议您这样做:

  1. 使用线程模块在单独的线程中运行更新功能,这不是有效的方法,但是如果卡住,则可以继续运行其余代码。
  2. 使用python请求库检查任何网站,然后在进行更新之前检查响应代码。

线程代码如下所示:

from threading import Thread
from time import sleep

def update():
    global update_result
    update_result = ports_flayer.edit_features(updates=[sfo_edit])

try:
    update_result = None
    t1 = Thread(target=update)
    t1.daemon = True  # mark our thread as a daemon
    t1.start()
    sleep(10)  # wait 10 seconds then timeout, adjust according to your needs
    if update_result == None:
        print('failed to update')
except Exception as e:
    print(e)