API python example, alternative asynchronous

Post here any issues with the API or feature requests
Post Reply
AlfredENeumann
Posts: 12
Joined: Mon Mar 14, 2016 1:30 pm

API python example, alternative asynchronous

Post 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
User avatar
Kode
Site Admin
Site Admin
Posts: 353
Joined: Wed Dec 18, 2013 11:34 am

Re: API python example, alternative asynchronous

Post 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)
AlfredENeumann
Posts: 12
Joined: Mon Mar 14, 2016 1:30 pm

Re: API python example, alternative asynchronous

Post 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.
Post Reply