While loop makes the browser freeze with Brython

谁说胖子不能爱 提交于 2020-03-21 06:56:11

问题


I’m trying to get the response of an api request made with ajax.ajax() and the response is stored into ['apiResponse'] in the HTML5 Local Storage (but the rest of the python function processes without waiting for it be put into localStorage).

Because of this I need to wait for it before getting the response and I thought I could do what I did below for the program to wait before it proceed.

Unfortunately the browser seems to freeze every time I put a while loop...

If someone know how to make Brython and the browser to stop freezing or another method to do what I wanna do...

(It would really help me as it’s the only step before succeeding getting Spotify api requests response)


from browser import ajax #to make requests
from browser.local_storage import storage as localStorage #to access HTML5 Local Storage

import json #to convert a json-like string into a Python Dict

#Request to the API
def on_complete(req):
    if req.status==200 or req.status==0:
        localStorage['apiResponse'] = req.text
    else:
        print("An error occured while asking Spotify for data")

def apiRequest(requestUrl, requestMethod):
    req = ajax.ajax()
    req.bind('complete', on_complete)
    req.open(requestMethod, requestUrl, True)
    req.set_header('Authorization', localStorage['header'])
    req.send()

def response():
    while localStorage['apiResponse'] == '':
        continue
    print('done')
    return json.loads(localStorage['apiResponse'])

Thanks in advance!

来源:https://stackoverflow.com/questions/60593343/while-loop-makes-the-browser-freeze-with-brython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!