Latency for API call to a Python Function

MRAB python at mrabarnett.plus.com
Tue Aug 29 11:47:32 EDT 2017


On 2017-08-29 16:22, shazianusra at gmail.com wrote:
> Hi,
> 
> I have an issue with one piece of code in Python that when my API using Nginx/Uwsgi/EC2 AWS calls this function it causes latency. I need help to figure out if there is I am doing something wrong with this particular function. Rest of the code calls for DB are working fine and no issues. But only this piece of code is causing latency.
> 
> def getvideos(self, listitem):
> channelId = ""
> returnVideos,dictData = {},{}
> mode = 0
> channelId = listitem[KEY_CHANNELID]
> if KEY_CHANNELMODE in listitem :
> mode = int(listitem[KEY_CHANNELMODE])
> filteredVideos = []
> 
> if mode == 0:
> filteredVideos = self.getFilteredVideos(listitem)
> 
> if mode == 1:
> filteredVideos = self.getFocus(listitem)
> 
> if mode == 2:
> filteredVideos = self.getFavourites(listitem)
> newFilteredVideos = []
> 
> print "Returning videos : ",filteredVideos
> if filteredVideos == None or len(filteredVideos) == 0:
> print "\n\n\n\tWe are returning NO filteredVideos !"
> #filteredVideos = list(set(filteredVideos))
> dictData[KEY_SERVICE] = SERVICE_GETVIDEOS
> dictData[KEY_CHANNELID] = str(channelId)	
> if len(filteredVideos) > 0 :
> dictData[KEY_VIDEOS] = self.returnVideos(filteredVideos)
> else :
> dictData[KEY_VIDEOS] = {}
> dictData[KEY_VIDEOS][KEY_STATUS] = 'No Videos'
> returnVideos[TAG_IRON] = dictData
> return json.dumps(returnVideos)
> 
> Looking forward,
> 
> Regards,
> Shazia
> 
It's difficult to read the code because it's not indented correctly.

At first glance, the latency might not be in this method itself, but in 
one of the other methods it calls.

Another possibility is that somewhere you're using a list where a set or 
dict might be better, e.g. doing a search (it'll be a linear search on a 
list).



More information about the Python-list mailing list