1
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

This is the example that I have shown in which I am trying to update feature layer. Actually I am updating the records in a loop so there are many records. The problem is in the case "Internet Connection" is just shot down It get stuck to the function edit_features.

So there is no way It could go to except and continue the flow.

I just have to ctrl+c to stop script execution because it got hanged and edit_features() function. What can I do?

Nimish Bansal
  • 1,719
  • 4
  • 20
  • 37

1 Answers1

2

If I am in your situation i would search arcgis API docs for setting connection timeout, if you can't find any i suggest:

  1. use threading module to run update function in separate thread, it is not the efficient way but in case if it gets stuck, you can continue running your remaining code.
  2. use python requests library to check any website and you check for response code before making the update.

code for threading will look like:

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)
Mahmoud Elshahat
  • 1,873
  • 10
  • 24
  • I think probably 1st option will be better though it can create several threads that never finishes, that might I think can have a big drawback(though I am not sure) if I have huge no of records. But second option cannot be the solution since edit_features can take its own time.so even if connectivity is there at time of request that you mentioned, its a great possibility that connectivity goes off even after edit_features() have been called and before its completion(of function edit_features() ) connectivity goes off then it will make it hanged – Nimish Bansal Apr 22 '19 at 02:49
  • @NimishBansal you are right, the only reliable solution should be provided by the `arcgis` API itself, there should be timeout option, if you can't find any there is some ways to kill hang threads examples in this [link](https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/) – Mahmoud Elshahat Apr 22 '19 at 03:22
  • yes right actually I was also looking the same and while searching it seems instead of killing thread, process killing is safer, according to this answer https://stackoverflow.com/a/25027221/7698247. So I think just instead of thread I should use process and kill the hanged process – Nimish Bansal Apr 22 '19 at 03:26