【问题标题】:Hanged while Executing function if Connection to Internet is Lost如果与 Internet 的连接丢失,则在执行功能时挂起
【发布时间】:2019-04-21 17:29:26
【问题描述】:
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 Connection”刚刚被击落的情况下它卡在了edit_features函数上。

所以没有办法它可以去except并继续流程。

我只需要 ctrl+c 停止脚本执行,因为它被挂起和 edit_features() 函数。我能做什么?

【问题讨论】:

    标签: python arcgis


    【解决方案1】:

    如果我遇到你的情况,我会搜索 arcgis API 文档以设置连接超时,如果你找不到我建议的任何内容:

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

    线程代码如下所示:

    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)
    

    【讨论】:

    • 我认为第一个选项可能会更好,尽管它可以创建几个永远不会完成的线程,如果我没有大量记录,我认为这可能会有一个很大的缺点(尽管我不确定)。但是第二个选项不能成为解决方案,因为 edit_features 可能需要自己的时间。所以即使在您提到的请求时存在连接,即使在调用 edit_features() 并且在完成之前连接也很有可能断开(函数 edit_features() ) 连接断开然后它会挂起
    • @NimishBansal 你是对的,唯一可靠的解决方案应该由arcgis API 本身提供,应该有超时选项,如果你找不到任何方法可以杀死挂起线程link 中的示例
    • 是的,实际上我看起来也一样,根据这个答案stackoverflow.com/a/25027221/7698247,在搜索时似乎不是杀死线程,进程杀死更安全。所以我认为我应该使用进程而不是线程并杀死被挂起的进程
    猜你喜欢
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    相关资源
    最近更新 更多