我试图从themoviedb.com API返回结果。我测量了整个python函数需要0.3s,但javascript在3-5s之后生成表,怎么可能?
烧瓶中的API调用功能
def whispererSeries(partOfName):
# ODESLANI REQUEST NA API PRO ODPOVIDAJICI SERIALY
# API CALL
url = URL('https://api.themoviedb.org/3/search/tv?api_key=' + config['API_KEY'] + '&language=en-US&query=' + str(
partOfName) + '&page=1')
http = HTTPClient(url.host)
response = http.get(url.request_uri)
body = json.loads(response.read().decode('utf-8'))
if response.status_code == 200:
# VRACEJICI SE HODNOTA
# RETURN ARRAY OF DICTIONARIES
valToReturn = []
if (len(body['results']) > config['FIND_MAX_RESULTS']):
for i in range(config['FIND_MAX_RESULTS']):
valToReturn.append({
'Name': body['results'][i]['name'],
'Url': getUrlOfSerie(str(body['results'][i]['id']))
})
elif (len(body['results']) > 0):
for i in range(len(body['results'])):
valToReturn.append(body['results'][i]['name'])
else:
valToReturn = []
return jsonify(valToReturn)
else:
print('Error: %r') % (response.status_code)
在jQuery中我调用python函数并将返回值附加到表。
// ODELSANI POZADAVKU NA SERVER
// CALL SERVER FUNCTION
$.ajax({
type: "POST",
contentType: 'application/json;charset=UTF-8',
// ODESLANI HODNOTY INPUTU
data: JSON.stringify($('.find-input').val()),
// CILOVA ADRESA
// ROUTE
url: '/indexWhisperer',
success: (response)=> {
console.log(response)
// VYCISTENI OD PREDCHOZICH VYSLEDKU
// CLEAR FROM PREVIOUS RESULTS
$('.founded-results tbody').empty()
// NASTAVENI VIDITELNOSTI TABULKY A PRIDANI VYSLEDKU
// MAKE TABLE VISIBLE AND APPEND RESULTS
response.forEach((item, index, array)=>{
$('.founded-results').css({'visibility': 'visible'})
$('.founded-results tbody').append('<tr><td><a href="' + item.Url + '">' + item.Name + '<a/></td></tr>');
})
},
error: (error)=>{
throw error
}
})
答案 0 :(得分:0)
刚刚建立了一个MySQL数据库,从api插入数据,现在它的速度要快得多。