[SOLVED] Python library blocking forever when server does not respond


#1

hello,
I am using the python library to update ubidots variables.
Since two days it works fine, but today I observe slow answering times of the ubidots servers, and sometimes the python scripts are hanging because the api calls never return.
What can we do to recover from network or server problems so that the script can exit, also when the api calls were unsuccessful?
thank you!


#2

Greetings, you may use timer objects to set a max time to run your scripts: https://docs.python.org/3/library/threading.html#timer-objects

Regards


#3

great thanks!
Here a hint for others who need the same:

import multiprocessing
from ubidots import ApiClient
...
#following code block "action" is started as thread with max runtime, here: 30 seconds`
def action():
  # Create an ubidots ApiClient object
  api = ApiClient(token='yourtoken')
  # Get a Ubidots Variable lat/long
  variable = api.get_variable('yourvariable')
  response = variable.save_value({'value': yourvalue})

p = multiprocessing.Process(target=action, name="action", args=())
p.start()
p.join(30)
if p.is_alive():
    print "thread is still running... let's kill it..."
    p.terminate()
    p.join()
else:
    print "ok: thread finished in time.."


...

#4

Thanks for sharing your solution!

Regards