API python example, alternative asynchronous
Posted: Mon Mar 21, 2016 10:27 pm
Hi all,
thanks again for the api, and thanks also for the examples which were a good way to start.
However, I think the python example is not the best way to realize the api. Yesterday, the server seemed to be very busy. It took up to 25 seconds until I got a response from the server (search for artist with mbid).
In fact, the waiting time is not a problem at all, but when programming it according to the example using urllib2.urlopen(), during this waiting time the application is completely stuck, as it is waiting for a response or the timeout.
A better way - to my opinion - is to use pythons twisted mechanism which loads the data asynchronous, thus it does not matter how long it takes until an answer is provided - the application is able to continue with other tasks in the meanwhile. Maybe this helps other people also who do not have (like me) too much experience with python. So I attach the small bit of code which is neccessary to (in my case) get the dowload urls for artist pics....
Have fun
Alfred
thanks again for the api, and thanks also for the examples which were a good way to start.
However, I think the python example is not the best way to realize the api. Yesterday, the server seemed to be very busy. It took up to 25 seconds until I got a response from the server (search for artist with mbid).
In fact, the waiting time is not a problem at all, but when programming it according to the example using urllib2.urlopen(), during this waiting time the application is completely stuck, as it is waiting for a response or the timeout.
A better way - to my opinion - is to use pythons twisted mechanism which loads the data asynchronous, thus it does not matter how long it takes until an answer is provided - the application is able to continue with other tasks in the meanwhile. Maybe this helps other people also who do not have (like me) too much experience with python. So I attach the small bit of code which is neccessary to (in my case) get the dowload urls for artist pics....
Have fun
Alfred
Code: Select all
from twisted.web.client import getPage
import json
....
getPage(url, timeout=10).addCallback(self.getFanartParseJson).addErrback(self.getFanartFailed)
...
def getFanartParseJson(self, resultString):
jsonDict = json.loads(resultString)
if len(jsonDict["artistbackground"])== 0: return
for pic in jsonDict["artistbackground"]:
self.artistBgPicsList.append(pic["url"])
def getFanartFailed(self,result):
pass #some reaction