0

我正在尝试从 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
        }
    })
4

1 回答 1

0

刚刚做了一个 MySQL 数据库,从 api 插入数据,现在速度快了很多。

于 2017-02-06T07:19:34.943 回答