Page 1 of 1

API python example, alternative asynchronous

Posted: Mon Mar 21, 2016 10:27 pm
by AlfredENeumann
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

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

Re: API python example, alternative asynchronous

Posted: Tue Mar 22, 2016 9:32 am
by Kode
Thanks Alfred, the examples are dynamically generated by apiary.

25 seconds is a long time o_O, what time was this? (GMT/UTC)

Re: API python example, alternative asynchronous

Posted: Fri Mar 25, 2016 11:45 pm
by AlfredENeumann
Hi Kode,
sorry for the late answer, I was not expecting an answer...
Unfortunately, as I am testing quite a lot right now, I deleted all my logs from Sunday, March 20, already. But, as I remember, this was at (about) 11:00 .. 13:00 UTC.
And yes, this was exceptionally long; normally it is much faster.